Modifiers in C#

There are five types of access modifiers being used:

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

We will discuss each of these in detail.

  • Public

    This access modifier allows a member/ object to be accessed anywhere within or across assemblies. This modifier grants the permission to modify the object by an external entity that is using it. Hence, this modifier has the lowest level of access restriction.

  • Private

    This access modifier restricts any object to use a member/object outside the class. With the highest level of access restriction, this modifier is generally used when the design does not allow modification to the defined behavior of member/object.

  • Protected

    In case of inherited members, this modifier plays a key role in allowing the child class to access the members of base class declared as protected.

  • Internal

    Within the same assembly, the members with 'internal' modifiers can be accessed across classes/ namespaces (provided they are in the same assembly).

  • Protected Internal

    This modifier works for the inherited members within an assembly. Unlike the internal modifier, this modifier allows an object to be accessed if only it is inherited and present in the same assembly as that of base class.
Ebook Download
View all
Learn
View all