What Does Access Modifiers Mean?
Access modifiers are keywords used to specify the accessibility of a class (or type) and its members. These modifiers can be used from code inside or outside the current application.
Access modifiers in .NET are used to control the accessibility of each of the members of a type from different possible areas of code. This can be handled from within the current assembly or outside it. An assembly represents a logical unit of functionality and consists of types and resources located in one or more files.
The purpose of using access modifiers is to implement encapsulation, which separates the interface of a type from its implementation. With this, the following benefits can be derived:
- Prevention of access to the internal data set by users to invalid state.
- Provision for changes to internal implementation of the types without affecting the components using it.
- Reduction in complexity of the system by reducing the interdependencies between software components.
Techopedia Explains Access Modifiers
The .NET framework provides an option of having five types of access modifiers:
- Private – code within the type can only access the members of that type, and hence accessibility is limited to current type
- Public – code from anywhere within the current assembly, or another assembly that references it, can access the members of the type, and hence allows accessibility from anywhere
- Protected – code within the type, or its derived classes, can access the members of the type and hence accessibility is limited to current type and derived classes
- Internal – code in the current assembly, but not from another assembly, can access the members of the type, hence accessibility is limited to current assembly
- Protected Internal – code in the current assembly can access the members of the type and also from the assembly that references it. Hence, accessibility is from derived classes in the current assembly, and must take place through an instance of derived class type in the assembly referencing it
There are several rules that apply to the access modifiers:
- When there is no access modifier specified to type members, the default access level is private and internal.
- There are no access modifiers allowed for namespaces, since they are public.
- The nested classes and struct members declared within a type are, to the containing class, private by default.
- Struct members cannot be declared protected since it does not support inheritance.
- Destructors cannot have access modifiers.
- Derived type cannot have greater accessibility than its base type.
- The member of a containing type should have accessibility lesser than that of its containing type. This can be illustrated with an example: A public method in a containing type cannot have “A” as a parameter, if type A is not in public visibility.
- Interfaces are declared public and internal, and cannot have other access modifiers, since interfaces are mainly used for access by classes to derive from it.
- Access modifiers are used not only to class members, but also to other code constructs with the same intention.