C#名称空间中的枚举与类中的枚举有什么区别 [英] C# what difference enum in namespace than enum inside a class
本文介绍了C#名称空间中的枚举与类中的枚举有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对下面的枚举有疑问:
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
相对于
MyEnums.Enum1
(在两种情况下,都假定名称空间被using
指令覆盖)
(in both cases the namespace is assumed to be covered with a using
directive)
但是,将其包含在类中还可以使您以不同的方式应用可访问性-您可以拥有仅在该类中可用的private
枚举,而命名空间中的枚举必须为public
或internal
.
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
.
这篇关于C#名称空间中的枚举与类中的枚举有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文