Each declared item has an accessibility level.. When access to an entity is allowed, the entity is said to be accessible. Conversely, when access to an entity is disallowed, the entity is said to be inaccessible. Accessibility does not change the scope of an entity's name. The accessibility domain of a declaration is the set of all contexts in which the declared entity is accessible.
The five access levels are public, protected, friend, protected friend, and private. The most permissive access level is public, and the four other access levels are all subsets of public access. The least permissive access level is private, and the four other access levels are all supersets of private access.
The access level for a declaration is specified via an optional access modifier, which can be can be Public
, Protected
, Friend
, Private
or the combination of Protected
and Friend
. If no access modifier is specified, a default access level is used depending on the declaration context; the permitted access levels also depend on the declaration context.
Public
modifier have public access. There are no restrictions on the use of public entities.Protected
modifier have protected access. Protected access can only be specified on members of classes (both regular type members and nested classes), although there are different access rules for the two. A protected type member contained in a class is accessible to entities contained in a derived class, provided the access takes place through the derived class. A protected type nested in a class is accessible to items contained in a derived class, provided the access takes place through the base class (since nested types are not inherited). Protected access is not a superset of friend access.Friend
modifier have friend access. An entity with friend access is accessible only within the assembly that contains the entity declaration.Protected Friend
modifiers have the union of protected and friend accessibility. That is they are accessible from all code within the same assembly and they are accessible from all derived classes.Private
modifier have private access. A private entity is accessible only within its declaration context, including any nested entities.There is never any restriction on the level of accessibility specified in a declaration. For example, a type declared with Private
access may contain a type member with Public
access.
Public
| Protected
| Friend
| Private