Access modifiers

Access modifiers enable you to declare the accessibility of a member or type. Access modifiers in C# are: public, protected, internal, protected internal and private.

Below is a table describing the accessibility limits of each access modifier:

Access Modifier Accessibility
public Almost anywhere: inside and outside the defining class, derived classes, inside the assembly and outside the assembly if referenced.
protected Only to the defining class and any derived classes both inside and outside the assembly
internal Anywhere inside the assembly but not outside the assembly. If a class inside a name space doesn’t have a modifier (public and internal are the only allowed modifiers for a class), it is internal by default.
protected internal Combines accessibility of both protected and internal. Accessible from anywhere in the assembly and only to derived classes outside the assembly.
private Only inside the defining class with no exceptions and is the most strictest form available. All class members are private by default.

Access modifiers in the order of decreasing accessibility:
public > protected internal > internal > protected > private

Below is some sample code to explain what the access modifiers mean for class members.

Some Observations:
  • public, protected internal and internal all have same accessibility inside the assembly.
  • protected internal members can be accessed from outside the assembly only from a subclass of the class that declared the members.  Note: If you want to make a member internally public but externally protected then use this modifier.
  • internal members are accessible from anywhere inside the assembly.
  • protected members are accessible from within the class and in derived classes only. The derived class can be inside or outside the assembly. Note: If you want to make a member accessible to derived classes and only inside the assembly (internally protected), you should make the class internal and the member protected.