Access Modifiers in C#

C#

4.00 ( 1 votes) | 993

In this article, we will talk about access modifiers in C#. Let`s get started.

Introduction:

Encapsulation is one of the pillars of object oriented programming. Encapsulation means ability to hide the unnecessary implementation details from object user. Encapsulation is implemented by using access modifier. Access modifier defines the visibility of type as well as members of type to other part of the application. There are five access modifiers available in C#: 

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

Let`s go through each one of them.

1. Public:

This access modifier can be applied for types or type members. There is no restriction on accessing the public type or type members. It is the default access modifier for enum and interface members.

2. Private:

This access modifier can be applied to type members or nested types. Private members can only be accessed within the body of the class or struct in which they are defined. Private is the default access modifier for all type members.

3. Protected:

This access modifier can be applied to type members or nested types. Protected members can only be access within the class in which they are declared or by derived classes. Other part of the application cannot access the protected members using C# dot operator.

4. Internal:

This access modifier can be applied for types or type members. Internal types or members are only accessible within the assembly in which they are declared. Internal is the default access modifier for all types like class, interface etc. For example if we define a class within  .Net class library with internal access modifier, then other assembly will not be able to access that class.

5. Protected Internal:

This access modifier can be applied to type members or nested types. When we declare a member as a protected internal member, then that member is only accessible within the assembly in which it is declared or by derived classes.

Conclusion:

In this article, we talked about different access modifiers available in C# and difference between them. I hope you enjoyed reading the article.

Happy Coding!!!

Manoj Kulkarni - Dotnetcontext

Manoj Kulkarni

I am programmer, passionate blogger and foodie. I live in Nashik. I am a .Net developer. I like to learn new technologies to keep my self-updated and share that knowledge with my blog readers, friends and colleague.

2 Comments


Sandip Pawar - Dotnetcontext

Sandip PawarSunday, February 28, 2016 2:49 AM

Thanks for the post it helped me a lot

Manoj Kulkarni - Dotnetcontext

Manoj KulkarniSunday, February 28, 2016 2:49 AM

Thank you for the feedback Sandip

Add a new comment