Access Modifiers In C#

In C#, basically five types of Access Modifiers are used, as defined below.

  • private
  • protected
  • public
  • internal
  • protected internal

Private access modifiers are used when we try to protect the data and methods from being accessed outside of the class. If we want to access private methods of a class, then we need to get it called from the same class methods which is not private. Our utmost attempt should be to make everything private.

Public access modifiers are the most basic types of modifiers which are used more frequently by individuals but we should not try to make everything public. In public, all the data members and methods are accessed by everyone within and outside the class.

Protected access modifiers are the types of modifiers which are accessible to the derived class in same or different assembly. Protected internal is a different access modifiers so we should not be confused about it.

Internal access modifiers are a new type which means that the members or methods of a class are accessible to all of its derived classes or other classes as long as they are in the same assembly. The difference between the public and internal is just that public is accessible to all within and outside the other assembly whereas internal is just accessible within the same assembly.

Protected internal is the last access modifier used in C#. It is a combination of actually two different modifiers -  protected and internal. It means that the members or methods are accessed to the derived class of the same assembly not the other assembly.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now