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

后端 未结 4 382
暗喜
暗喜 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 21:00

    Put the enum definition inside of the Program.cs file, but outside of the Program class. This will make the enum type globally accessible without having to reference the class name.

    namespace YourApp
    {    
        enum Direction { north, east, south, west };
    
        static class Program
        {   
        }
    }
    

    Then you can access it anywhere in any class within the same namespace without the need to specify the class name like this:

    Direction d;
    d = Direction.north;
    

提交回复
热议问题