What is Access Modifiers in C#?

Introduction

The visibility and accessibility of classes, methods, fields, properties, and other members inside a C# program can be specified using keywords called access modifiers. They regulate which program components have access to and can communicate with a specific member or type. Code readability, security, and integrity are all dependent on the encapsulation and definition of abstraction provided by access modifiers.

There are five primary access modifiers in C#.

  1. Public Modifier
  2. Private Modifier
  3. Protected Modifier
  4. Internal Modifier
  5. Protected Internal Modifier

Let's discuss each one in detail.

Public Modifier

Types and members declared as public can be accessed from any code in the same assembly as well as from other assemblies. This is the most permissive access level.

  • Public members or types are accessible from any code, from inside the same assembly, as well as from different assemblies.
  • When you want a member to be reachable from outside code or other program components, use this modifier.

Private Modifier

Private members can only be accessed within the contained type. It is not possible to access them from another type.

  • Only members who have been tagged as private can access them within the contained type.
  • Use this modification to restrict access from outside the class and conceal implementation details.

Protected Modifiers

Accessible within the containing type and its derived types (subclasses) are members designated as protected. A code that is not part of the containing type or any of its derived types cannot access them.

  • Accessible within the containing type and its derived types are members designated as protected.
  • When you wish to make a member available for use only within subclasses and not to external code, use this modifier.

Internal Modifiers

Internal members and types can only be accessed from within the same assembly; they cannot be accessed from outside. Implementation details are frequently encapsulated within a library or application using this access modifier.

  • Internal members are only accessible from within the same assembly; they cannot be accessed from the outside.
  • To contain implementation details inside the assembly, use this modifier.

Protected Internal Modifiers

Accessible within the containing type, its derived types, and any code within the same assembly are members designated as protected internal. The behaviors of both internal and protected are combined in this modifier.

  • Accessible within the containing type, its derived types, and any code within the same assembly are members designated as protected internal.
  • When you wish to grant access to derived types inside the assembly but not outside of it, use this modifier.

Conclusion

To sum up, access modifiers in C# are essential for managing the encapsulation and abstraction of your code by regulating the visibility and accessibility of members and types. You can improve the security, maintainability, and extensibility of your C# programs by selecting the right access modifier, which makes sure that members are only accessed in the intended and regulated ways.

Feel free to drop a comment if you have any queries.

Thanks,

Mukesh


Similar Articles