简体   繁体   中英

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

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM