C# what difference enum in namespace than enum inside a class

吃可爱长大的小学妹 提交于 2020-04-06 05:51:37

问题


i have a question about enums that the codes are below:

namespace space
{
    public enum MyEnums
    {
        Enum1,Enum2,...
    }
}

namespace space
{
    public class MyClass
    {
        public enum MyEnums
        {
            Enum1,Enum2,...
        }
    }
}

whats difference and how to use them?


回答1:


Well syntactically the only difference is that you'd preface the enum type with the containing class:

MyClass.MyEnums.Enum1 

versus just

MyEnums.Enum1 

(in both cases the namespace is assumed to be covered with a using directive)

However, containing it within the class also lets you apply accessibility differently - you could have a private enum that is only usable within that class, while an enum within a namespace must be either public or internal.



来源:https://stackoverflow.com/questions/24784312/c-sharp-what-difference-enum-in-namespace-than-enum-inside-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!