How to get global access to enum types in C#?

后端 未结 4 381
暗喜
暗喜 2021-02-03 20:14

This is probably a stupid question, but I can\'t seem to do it. I want to set up some enums in one class like this:

public enum Direction { north, east, south,          


        
4条回答
  •  别跟我提以往
    2021-02-03 20:57

    You can do one of two things.

    1- Move the declaration of the enum outside of the class

    Today you probably have something like this

    public class ClassName
    {
      public enum Direction
      {
        north, south, east, west
      }
      // ... Other class members etc.
    }
    

    Which will change to

    public class ClassName
    {      
      // ... Other class members etc.
    }
    
    // Enum declared outside of the class
    public enum Direction
    {
      north, south, east, west
    }
    

    2- Reference the enum using the class name

    ClassName.Direction.north
    

    Eg.

    public void changeDirection(ClassName.Direction direction) { 
       dir = direction; 
    }
    

    Where ClassName is the name of the class that you declared the enum in.

提交回复
热议问题