Access Modifiers
Access modifiers are keywords used to specify the declared accessibility of types and type members. The four access modifiers are discussed in the table below:
Access Modifier | Meaning |
public | public is an access modifier for types and type members. This is the most permissive access level as there are no restrictions on accessing a public type or type member. |
internal | internal is an access modifier for types and type members. internal members are accessible only within file of the same assembly. It is an error to reference an internal type or internal type member outside the assembly within which it was declared. A common use of internal access is in component-based development because it enables a group of components to interact in a private matter without being exposed to the outer world. For example, a Data Access Layer could have several classes with internal members that are only used by the DAL. |
protected | protected is an access modifier for type members only. A protected member is only accessible within the body of the containing type and from within any classes derived from the containing type. |
private | private is an access modifier for type members only. private access is the least accessible and such members are only accessible within the body of the containing type.
Note that nested types within the same containing body can also access those private members.
|
Accessibility Levels
The four access modifiers listed above result in five accessibility levels shown below. Note how certain accessibility levels are not permitted depending on the context in which an access modifiers was used:
Accessibility Level | Meaning | Applies To Namespaces | Applies To Types | Applies To Type Members |
public | Access is not restricted. | Yes | Yes | Yes |
internal | Access is limited to the current assembly (project). | No | Yes | Yes |
protected | Access is limited to the containing class, and classes derived from the containing class | No | No | Yes |
internal protected | Access is limited to the current assembly (project), the containing class, and classes derived from the containing class | No | No | Yes |
private | Access is limited to the containing type. | No | No | Yes |
The following table illustrates accessibility levels for namespaces, types, and type members. Note that namespace elements (i.e., classes, structs, interfaces and enum) can only have public or internaldeclared accessibility. Access modifiers private, protected, and protected internal are not allowed on namespace members.
No comments:
Post a Comment