woz*_*zar -2 c# enums access-modifiers
我认为这是不可能的,但不知何故你可以添加修饰符以enum不同于public.
这段代码工作得很好,并充当实际的私有成员(在包含 class 之外无法访问C:
namespace N {
public class C {
private enum E { ... }
}
}
Run Code Online (Sandbox Code Playgroud)
简短回答:尽可能少的访问(参见 Jon Skeet 的回答)。
长答案:
非嵌套类型、枚举和委托可访问性(可能只有内部或公共可访问性)
Run Code Online (Sandbox Code Playgroud)| Default | Permitted declared accessibilities ------------------------------------------------------------------ namespace | public | none (always implicitly public) enum | public | none (always implicitly public) interface | internal | public, internal class | internal | public, internal struct | internal | public, internal delegate | internal | public, internal嵌套类型和成员可访问性
Run Code Online (Sandbox Code Playgroud)| Default | Permitted declared accessibilities ------------------------------------------------------------------ namespace | public | none (always implicitly public) enum | public | none (always implicitly public) interface | public | none class | private | All¹ struct | private | public, internal, private² delegate | private | All¹ constructor | private | All¹ interface member | public | none (always implicitly public) method | private | All¹ field | private | All¹ user-defined operator| none | public (must be declared public)¹ 所有 === 公共、受保护、内部、私有、受保护的内部
² 结构不能从结构或类继承(尽管它们可以,接口),因此 protected 不是有效的修饰符
嵌套类型的可访问性取决于其可访问性域,该域由成员声明的可访问性和直接包含类型的可访问性域决定。但是,嵌套类型的可访问域不能超过包含类型的可访问域。
注意:CIL 还提供了受保护和内部(与现有的受保护的“或”内部相反),但据我所知,目前在 C# 中不可用。
看:
http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx
http://msdn.microsoft.com/en-us/library/ms173121.aspx
http://msdn.microsoft.com/en- us/library/cx03xt0t.aspx (我喜欢微软的URIs...)
那是描述an成员的可访问性enum,而不是它enum本身。枚举的所有成员都不能被赋予可访问性修饰符,并且始终是公开的。
所以你不能这样做:
public enum Foo
{
public SomeValue = 1,
internal AnotherValue = 2,
}
Run Code Online (Sandbox Code Playgroud)
SomeValue并且AnotherValue将始终隐式公开。
| 归档时间: |
|
| 查看次数: |
357 次 |
| 最近记录: |