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

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

    It's public, but defining an enum inside a class makes it an inner type of that class. For instance:

    namespace MyNamespace
    {
        public class Foo
        {
            public enum MyEnum { One, Two, Three }
        }
    }
    

    In order to access this enum from another class in the MyNamespace namespace, you have to reference it as Foo.MyEnum, not just MyEnum. If you want to reference it as simply MyEnum, declare it just inside the namespace rather than inside the class:

    namespace MyNamespace
    {
        public class Foo { ... }
    
        public enum MyEnum { One, Two, Three }
    }
    

提交回复
热议问题