Access Modifiers private, public, protected, internal in c#
Class access modifiers in the C# programming language are reserved keywords that define the current accessibility or visibility of a user-defined class’s data type and class members, such as class fields, properties, methods, etc. Access modifiers within a class are controlled through these access modifiers, allowing access and control of existing class data elements or structure elements from outside their root class or group collection.

There are four main types of access modifiers in C# programming.
- Private access modifiers.
- Public access modifiers.
- Protected access modifiers.
- Internal access modifiers.
Each class access modifier defined above helps define the accessibility scope of a class element for an individual part element of a class or structure.
So, let’s explore the access modifier concept in C# programming.
Private Access Class Modifier.
- Private Access Class Modifier Scope – In a C# program, the private access class parameter members declared can only be accessed and called from within the containing root or parent class.
- Private Access Class Modifier Use Case – In a C# program, the private access class modifier is commonly used for class fields or user-defined class methods that should not be directly accessed externally within a class. This provides the encapsulation concept features within a class in the maximum order.
Example of a Private Access Class Modifier.
using System;
class Employee
{
private string emp_name; // here it is accessible only within the Employee class field member
private int emp_age; // here it is accessible only within the Employee class field member
// here we set the public class method to set the emp_name field value
public void SetName(string emp_name)
{
this.emp_name = emp_name;
}
// here we use Public class method to get emp_name field value
public string GetName()
{
return emp_name;
}
// here we define a Public class method to set emp_age field value
public void SetAge(int emp_age)
{
this.emp_age = emp_age;
}
// here we use a Public class method to get emp_age field value
public int GetAge()
{
return emp_age;
}
}
class program
{
static void Main(string[] args)
{
// here we Create object of Employee class
Employee employeesfirst = new Employee();
// here we Set values using public class methods
employeesfirst.SetName(“Bhavishi Deora”);
employeesfirst.SetAge(21);
// here it gets and displays employee class values
Console.WriteLine(“Employee Name is – ” + employeesfirst.GetName());
Console.WriteLine(“Employee Age is – ” + employeesfirst.GetAge());
}
}
Here in the Private Access Class Modifier example.
- Here in the Employee class, the fields emp_name and emp_age are defined as private class members and cannot be directly accessed externally within the Employee class. Similarly, the SetName() and GetName() function methods in the Employee class provide user-defined class methods with controlled access.
Public Access Class Modifier.
- Public Access Class Modifier Scope – Public class parameter data type members declared in a C# program can be accessed and controlled from anywhere within the class. Public class members are like universal data types; they are not subject to any call restrictions within the class program.
- Public Access Class Modifier Use Case – In a C# program, it is used for user-defined class members (class fields, methods, and properties) that need to be accessed from another class, just like a public data type class member. You can also access a public class member directly from outside a containing assembly class.
Example of a Public Access Class Modifier.
using System;
class Employee
{
public string Emp_Name; // here the public class member Emp_Name can be accessed from anywhere in the class
public int Emp_Age; // here the public class member Emp_Age can be accessed from anywhere in the class
// here we use the class method to display employee class information
public void EmployeeInfo()
{
Console.WriteLine($”\n Employee Name is – {Emp_Name} \n Employee Age is – {Emp_Age}”);
}
}
class program
{
static void Main(string[] args)
{
// here we Create an object for Employee class member
Employee employeesfirst = new Employee();
// here we Assign an employee Claas values
employeesfirst.Emp_Name = “Siddhi Deora”;
employeesfirst.Emp_Age = 19;
// here we use a class Call method
employeesfirst.EmployeeInfo();
}
}
Public Access Class Modifier Example Explanation.
- Here in the Employee class, the Emp_Name, Emp_Age, and EmployeeInfo() members are all defined as public, and can be directly accessed and controlled by any other class or namespace outside the Employee class.
Protected Class Access Modifier.
- Protected Class Access Modifier Scope – In a C# program, the protected class member data type variable element declared can be accessed and controlled only from within the existing class and derived class subclass element data type methods.
- Protected Class Access Modifier Use Case – In a C# program, the protected class access modifier is used when you want to provide access to the member element of a derived class within the existing class, but it cannot be accessed or controlled from outside that class hierarchy.
Example of a Protected Class Access Modifier.
using System;
class staff
{
protected string emp_name; // here it is accessible within this defined class and derived subclass member
protected int emp_age; // here it is accessible within this defined class and derived subclass member
// here we create a staff name class Constructor
public Staff(string emp_name, int emp_age)
{
this.emp_name = emp_name;
this.emp_age = emp_age;
}
}
class Employee: Staff
{
public string department;
// here we use employee name class Constructor
public Employee(string emp_name, int emp_age, string department) : base(emp_name, emp_age)
{
this.Department = department;
}
// Here we use the method to display employee information
public void EmployeeInfo()
{
Console.WriteLine($”\n Employee Name is – {emp_name} \n Employee Age is – {emp_age} \n Employee Department is – {Department}”);
}
}
class Program
{
static void Main(string[] args)
{
// Here we create an Employee class object
Employee emp1 = new Employee(“Harry Deora”, 24, “Ai Engineer”);
// Here we try to display employee member details
emp1.EmployeeInfo();
}
}
Here in the Protected Class Access Modifier example.
- In this program, the emp_name and emp_age class fields are defined as protected, meaning they can be directly accessed by the Employee class (which is derived from the Staff class). However, these class members and class data cannot be accessed from outside the class.
Internal Access Class Modifier.
- Internal Access Class Modifier Scope – Internal access class modifier members declared in a C# program can be accessed from within the same class assembly. However, these class members cannot be accessed or controlled from outside the class assembly.
- Internal Access Class Modifier Use Case – Internal access class modifiers are mostly used in C# programs for class members that are defined to be used only within the class assembly (project). However, they are not exposed to external class projects that reference the class’s assembly.
Example of Internal Access Class Modifier.
using System;
class Employee
{
internal string Emp_Name; // here Emp_Name field is accessible only within the same class assembly element
internal int Emp_Age; // here Emp_Age field Accessible only within the same class assembly element
// here we define a class Method to display employee information
internal void EmployeeInfo()
{
Console.WriteLine($”\n Employee Name is – {Emp_Name} \n Employee Age is – {Emp_Age}”);
}
}
class program
{
static void Main(string[] args)
{
// here we Create object of Employee class member
Employee employees = new Employee();
// here we assign employee class element values
employees.Emp_Name = “Siddhi Deora”;
employees.Emp_Age = 19;
// here we use Call class method
employees.EmployeeInfo();
}
}
Here in the Internal Access Class Modifier example.
- In this example, the Emp_Name, Emp_Age, and EmployeeInfo() members are internal class fields, so they can be accessed and controlled by any class within the same assembly. However, they cannot be accessed by any class in a different assembly, even if it references the current assembly.
Combining Multiple Access Modifiers in a Class.
In the C# programming language, you can group class access modifiers together in more complex program condition scenarios to provide detailed control over the accessibility of multiple class members and data elements. For example, a user-defined class might have a public method, but it can call and manage an internally private class field if needed.
Example of Multiple Access Modifiers access levels.
using System;
class Employee
{
private string Emp_name; // here emp_name class field only accessible within the Employee class
public int Emp_Age; // here Emp_Age class field accessible from anywhere in the class
protected string Location; // here Location class field accessible within the class and derived subclasses
internal string Email; // here email class field Accessible within the same class assembly
// here we use Public method to set emp_name
public void SetName(string updateName)
{
Emp_name = updateName;
}
// here we use Public method to set employee location
public void SetLocation(string updateLocation)
{
Location = updateLocation;
}
// here we use Method to display employee data information
public void EmployeeInfo()
{
Console.WriteLine($”\n Employee Name is – {Emp_name} \n Employee Age is – {Emp_Age} \n Employee Location is – {Location} \n Employee Email is – {Email}”);
}
}
class program
{
static void Main(string[] args)
{
// here we Create an object for Employee class
Employee employees = new Employee();
// Here we manually set values for the employee class member
employees.SetName(“Harry Deora”);
employees.Emp_Age = 24;
employees.SetLocation(“Mumbai, Maharastra”);
employees.Email = “harry@domain.com”;
// Here we display employee class information
employees.EmployeeInfo();
}
}
Multiple Access Modifiers access example Explanation.
- In the above program example, Emp_name is privately defined and cannot be accessed or controlled from outside the Employee class.
- Employee Emp_Age is a publicly defined class member or field, hence it can be accessed and controlled from anywhere in the current program class.
- Similarly, the Location class field is protected in nature, which means it can be accessed and controlled from within the Employee class and any class derived from Employee.
- Email is an internal class member data type, meaning it can only be accessed and controlled within the same class assembly.
Additional Class Access Modifiers.
- protected internal – Class members with this modifier defined in a C# program can be accessed and controlled by classes in the same assembly or derived classes, even if they are defined in another assembly.
- private protected – Class member data types with this modifier defined in a C# program can only be accessed and controlled by the containing class or derived classes in the same assembly.
Example of protected internal and private protected class members.
class Employee
{
protected internal string Emp_Name; // This can be accessible within the Employee class in the same assembly or derived classes
private protected string Location; // This can be accessible within the Employee class, within the same class and derived classes in the same assembly.
}
Detail Information About Class Access Modifiers in c#.
| Modifier | Each class Accessibility | Where to Use Case |
| private | Private class field Only used within the containing class member | Private class member helps us to manage Data encapsulation with (prevent external access) method |
| public | Public class member can be access from anywhere (no restrictions) inside everywhere class member | Public class member used to Expose members to external code or other assemblies |
| protected | Protected class member called or Containing class and derived classes with access | Protected class member Allow you to access in derived classes (inheritance) features |
| internal | Internal class member assign Within the same assembly (project) | Internal class member Expose to code within the same assembly but not to other assemblies |
Detailed Summary of Access Modifiers: private, public, protected, internal class element.
- private – Privately defined class fields provide access and control only within the containing class.
- public – Publicly defined class field members provide unrestricted direct access and control from anywhere within the current class.
- protected – A class member defined within a protected class provides access and control only within the containing class and derived class members.
- internal – This provides access within the current class within the same assembly.
- protected internal – A protected class member provides access within the same assembly or from a derived class.
- private protected – This current class provides access and control within the containing class and derived classes within the same assembly.
