问题
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