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

后端 未结 4 388
暗喜
暗喜 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:40

    Declare enum in the scope of a namespace like a class but not into a class:

    namespace MyApplication
    {
       public enum Direction { north, east, south, west };
    }
    

    In case enum is declared in the scope of a class, you have make this class public too:

    namespace MyApplication
    {
       public class MyClass
       {
          public enum Direction { north, east, south, west };
       }
    }
    

    Usage:

    MyClass.Direction dir = ...
    

提交回复
热议问题