This tutorial is about access modifiers in C#. The types of access modifiers are discussed in detail

Introduction

In c#, access modifiers define the scope of a class member. A class member refers to the properties and methods in a class. A program can contain more than one class and you may not want some members of the class to be accessible outside the class. A programmer can use access modifiers to implement encapsulation in C#.

Types of access modifiers
• public
• private
• protected
• internal
• protected internal

Step 1: Access Public Members
The public access modifier in C# allows a class to share its members with other classes within or outside. The following code snippet shows the use of public access modifier:
class Info
{
public int id;
public string name;
public int age;
public void PrintDetails()
{
Console.WriteLine("Id /t:"+id);
Console.WriteLine("Name /t:"+name);
Console.WriteLine("Age/t:" + age);
}
}

Hence if you call the member variables in the Main() method as follows:
Info myInfo = new Info();
myInfo.id = 101;
myInfo.name="user";
myInfo.age=24;

The variables can be accessed as they are public.

Step 2: Access Private Members
The private access modifier allows access only to the belonging class. So if you modify the above class to:
class Info
{
private int id;
public string name;
int age;
public void PrintDetails()
{
Console.WriteLine("Id /t:"+id);
Console.WriteLine("Name /t:"+name);
Console.WriteLine("Age/t:" + age);
}
}
When you build the program, an error pops up. The access is denied. The variable id cannot be accessed from any other class. However, name can be accessed as they are still public. By default, the access modifier for a member of a class is private. Hence age is defaulted to private access modifier.

Thus the private access modifier allows a class to hide its members from other class objects and functions. If a member is declared private, only the methods of that class can access the member. Even the instance of the class cannot access those members. Thus the data is hidden and can only be modified by the member functions of the class.

Step 3: Access Protected Members
The protected access modifier allows a class to hide its members from other classes and methods, except the child class. This access modifier is important while implementing the concept of inheritance. In the above example since there is no inheritance, a protected member will have the same visibility as that of a private member.

Step 4: Access Internal Members
Any member that is declared as internal can be accessed from any class or method defined within the application in which the member is defined. In C#, the access modifier by default for a class is internal.
class Info
{
private int id;
public string name;
internal int age;
public void PrintDetails()
{
Console.WriteLine("Id /t:"+id);
Console.WriteLine("Name /t:"+name);
Console.WriteLine("Age/t:" + age);
}
}
In the preceding code, the id is private and cannot be accessed outside the class. On the other hand, the age variable is internal. Hence it can be accessed from outside the class but inside the same application.

Step 5: Access Protected Internal Members
The protected internal access modifier allows a class to expose its member properties and methods to the containing class, child class, classes within the same application and derived classes outside the application. This access modifier gains importance while implementing inheritance.

Conclusion
The following table shows the visibility of the class members for the access modifiers in C#.