Depending on the context in which a member declaration takes place, only certain types of declared accessibility are permitted. Furthermore, when a member declaration does not include any access modifiers, the context in which the declaration takes place determines the default declared accessibility.
成员种类 | 默认可访问性 | 可以选择的可访问性 | 书写惯例及示例 |
---|---|---|---|
Namespaces | public | 无 | 不容许写 |
Class | private | public/protect/internal/Protected internal/private/ | public class |
Types | private | public or internal | 省略,如 int x; |
Struct | private | public, internal, or private | public struct a; |
Interface | public | 无 | 不容许写 |
Enumeration | public | 无 | 不容许写 |
- Namespaces implicitly have public declared accessibility. No access modifiers are allowed on namespace declarations.
- Types declared in compilation units or namespaces can have public or internal declared accessibility and default to internal declared accessibility.
- Class members can have any of the five kinds of declared accessibility and default to private declared accessibility. (Note that a type declared as a member of a class can have any of the five kinds of declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.)
- Struct members can have public, internal, or private declared accessibility and default to private declared accessibility because structs are implicitly sealed. Struct members introduced in a struct (that is, not inherited by that struct) cannot have protected or protected internal declared accessibility. (Note that a type declared as a member of a struct can have public, internal, or private declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.)
- Interface members implicitly have public declared accessibility. No access modifiers are allowed on interface member declarations.
- Enumeration members implicitly have public declared accessibility. No access modifiers are allowed on enumeration member declarations.