C#

Access Modifiers In C sharp

Access Modifiers are keywords that define the accessibility of a member, class or datatype in a program.
For example, a public class is accessible to everyone without any restrictions, while an internal class may be accessible to the assembly only.
There are 4 access modifiers (public, protected, internal, private) which defines the 6 accessibility levels as follows:
          
          1.      Public
          2.      Protected
          3.      Internal
          4.      Protected Internal
          5.      Private
          6.      Private protected

Why to use access modifiers?
Access modifiers are an integral part of object-oriented programming. Access modifiers are used to implement encapsulation of OOP. Access modifiers allow you to define who does or who doesn't have access to certain features.

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

The type or member can be accessed only by code in the same class or struct.

The type or member can be accessed only by code in the same class, or in a class that is derived from that class.

The type or member can be accessed by any code in the same assembly, but not from another assembly.

The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.

Private Protected
The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.

Class and Struct Accessibility
Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal.

NOTE:- Internal is the default if no access modifier is specified.

Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.
Derived classes cannot have greater accessibility than their base types. In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

Class and Struct Member Accessibility
Class members (including nested classes and structs) can be declared with any of the six types of access. Struct members cannot be declared as protected because structs do not support inheritance.
Normally, the accessibility of a member is not greater than the accessibility of the type that contains it. However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class.

Also, starting with C# 7.2, you can use the private protected access modifier to achieve the same result without need to make the containing class internal.

Other Types
Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.
Enumeration members are always public, and no access modifiers can be applied.
Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.

See also for more detail
        ·         Private
        ·         Public
        ·         Internal
        ·         Protected
        ·         Protected Internal
        ·         Private Protected


No comments:

Post a Comment