Access Modifiers in C#

“Access Modifiers are keywords in C# which are used to restrict avaibility of object,method,class and its members into the program or in application”.
These access modifiers are mainly used to define the scopes of types and members and prevent external programs from accidentally changing data.

Types of Access Modifiers in C# –

  • Public
  • Protected
  • Internal
  • Protected Internal
  • Private
  • Private Protected

Public Access Modifier-

The public access modifier allows unrestricted access to the class, method, or variable, including external code, from anywhere in the program.
Any code can access the class, method, or variable without restrictions.

Accessibility:
Any code in the same assembly or another assembly that references a type or member that has been declared public can access it.
The code is accessible to all classes.
The public members of a class can be accessed from any part of the code. As a result, they are the least safe. Any code logic can change the value, resulting in unexpected behaviour.So, extra care must be taken before making any object publicly available.

Protected Access Modifier-

The protected access modifier allows access to the class, method, or variable from within the class and any derived classes. It means that the class, method, or variable can be accessed by any code that is part of the class or any derived classes but not by external code.

Accessibility:

Protected members are only visible to their classes and their derived classes.
We can’t access protected members by the object of that class.

Internal Access Modifier-

The internal access modifier restricts access to the class, method, or variable within the same assembly.

Accessibility:

When a type or member is declared internal in C#, it can only be accessed by the current assembly and not by any other assemblies, according to the internal access modifier.

There is only current assembly access.

Example-

This example contains two files in two separate projects Program1.cs and Program.cs.  The first file contains an internal base class BaseClass. In the second file, an attempt to instantiate BaseClass will fail.

Protected Internal Access Modifier-

It is a combination of two other access modifiers: protected and internal.

The protected keyword means that a member (field, method, or property) is accessible only within its own class and any class derived from it. This means that the member is not accessible from outside the class or from non-derived classes.

The internal keyword means that a member is accessible within the same assembly (or module). This means that the member is not accessible from outside the assembly.

When you combine the protected and internal keywords using the protected internal modifier, you create a member that is accessible within its own assembly, as well as from any class that derives from the member’s class, whether the derived class is in the same assembly or not.

In other words, a protected internal member can be accessed from:

  • Any class within the same assembly.
  • Any derived class, regardless of whether it is in the same assembly or not.

Private Access Modifier-

It is used to restrict the access to a member (field, method, or property) to only the class in which it is declared. This means that the member cannot be accessed from outside the class, including any derived classes.

When you declare a member as private, it can only be accessed within the same class.

Accessibility:

The private keyword is just a member access modifier and cannot be explicitly used by classes or structures.
Nested types in the same body can also access these private members.
A compile-time error will be thrown when a private member is referenced outside of the class or structure in which it is declared.

Private Protected Access Modifier-

The private protected access modifier in C# is a combination of the private and protected access modifiers. It was introduced in C# 7.2 and is used to restrict access to a member to only the current class and any derived classes that are declared within the same assembly.

With the private protected access modifier, a member can only be accessed within its own class or any derived class that is declared within the same assembly. This means that it cannot be accessed from outside the assembly or from a derived class in a different assembly.