Declarations of members enable control over member access. The member visibility is established by the access level specified in its declaration combined with the access level of the containing type, if any. An access modifier is used to specify access level:
Access modifier | Description | Elements that can be declared using this modifier | Declaration context where a modifier can be used |
Public | Public access level. Member access is not limited. | classes, interfaces, enumerations, structures, delegates constants, variables, procedures and functions, properties, events |
class, namespace, structure |
Private | Private access level. Member access is limited to the containing class. | constants, variables, procedures and functions, properties, events |
class, structure |
Protected | Protected access level. Member access is limited to the containing class or types derived from the containing class. | constants, variables, procedures and functions, properties, events |
class |
Friend | Assembly access level. Member access is limited to the assembly (program) in which it is declared. | classes, interfaces, enumerations, structures, delegates, events |
class, namespace, structure |
Protected Friend | Protected assembly access level Member access is limited either to the current assembly or to the classes derived from the class containing this member, or to both conditions being satisfied. | constants, variables, procedures and functions, properties, events |
class |
Depending on the context, in which a member declaration takes place, only certain access modifiers are permitted. When a member declaration does not include any access modifiers, its visibility is determined by the default declared accessibility.
Namespaces implicitly have public declared access. No access modifiers are allowed on namespace declarations.
Types declared in compilation units or namespaces can have public or assembly declared access level. By default, they have assembly declared access level.
Class members can have any of the five kinds of declared access level and, by default, private declared access level.
Structure members can have public, assembly, or private declared access level because structures are implicitly sealed types. By default, they have private declared access level.
Interface members implicitly have public declared access level. 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.
Specific structures in the language require a type to be at least as accessible as a member or another type.
There are the following constraints:
The parent class of a class type must be at least as accessible as the class type itself.
The basic interface of an interface type must be at least as accessible as the interface type itself.
The return type and parameter types of a delegate type must be at least as accessible as the delegate type itself.
The type of a constant must be at least as accessible as the constant itself.
The type of a field and parameter types of a field must be at least as accessible as the field itself.
The return type and parameter types of a method must be at least as accessible as the method itself.
The type of a property must be at least as accessible as the property itself.
The type of an event must be at least as accessible as the event itself.
The parameter types of an instance constructor must be at least as accessible as the instance constructor itself.
See also: