Friday, November 11, 2011

Modifier

Modifier

Modifier

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 ModifierMeaning
publicpublic 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.
internalinternal 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.
protectedprotected 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.
privateprivate 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 LevelMeaningApplies To NamespacesApplies To TypesApplies To Type Members
publicAccess is not restricted.YesYesYes
internalAccess is limited to the current assembly (project).NoYesYes
protectedAccess is limited to the containing class, and classes derived from the containing classNoNoYes
internal protectedAccess is limited to the current assembly (project), the containing class, and classes derived from the containing classNoNoYes
privateAccess is limited to the containing type.NoNoYes
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. 
ContextDefault AccessibilityAllowed Declared AccessibilityDefault Member AccessibilityAllowed Declared Accessibility On Members
namespacepublicNoneInternalpublic
internal
classinternal (if top level)public
internal
privatepublic
protected
internal
internal protected
private
interfaceinternal (if top level)public
internal
publicNone
structinternal (if top level)public
internal
privatepublic
internal
private
enuminternal (if top level)public
internal
pubicNone

No comments:

Post a Comment