![]()
Access Modifiers can be used in everything defined in C#. These are class, struct, function, method, property, and all variables at the class level are used.
So, why do we use access Modifiers?
Because we want to Access Modifiers the accessibility of the definitions we have in our application from outside the code area where they are located.
C# provides five types of access Modifiers.
- Private
- Public
- Protected
- Internal
- Protected internal
Short table of access Modifiers

1- Private
Private Access Modifiers is used to specify private accessibility to the variable or function. It is most restrictive and accessible only within the body of class in which it is declared.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class Program { static void Main(string[] args) { Employee personel = new Employee(“Robert”, “White”); Console.WriteLine($“First Name => {personel.FirstName}”);//no access Console.WriteLine($“Last Name => {personel.LastName}”);//no access } } public class Employee { private string FirstName { get; set; } private string LastName { get; set; } private Employee(string FirstName, string LastName) { this.FirstName = FirstName; this.LastName = LastName; } } |
2- Public
The data accessible defined as public is completely accessible inside and outside the code block. So, there are no restrictions.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class Program { static void Main(string[] args) { Employee personel = new Employee(“Robert”, “White”); Console.WriteLine($“First Name => {personel.FirstName}”); Console.WriteLine($“Last Name => {personel.LastName}”); } } public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public Employee(string FirstName, string LastName) { this.FirstName = FirstName; this.LastName = LastName; } } |
Output:
First Name => Robert
Last Name => White
3-Protected
It is accessible within the class. It is also accessible within subclass or child class, in case of inheritance.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class Program { static void Main(string[] args) { Employee personel = new Employee(“Robert”, “White”); Console.WriteLine($“First Name => {personel.FirstName}”);//no access Console.WriteLine($“Last Name => {personel.LastName}”);//no access } } public class Employee { protected string FirstName { get; set; } protected string LastName { get; set; } protected Employee(string FirstName, string LastName) { this.FirstName = FirstName; this.LastName = LastName; } } |
Now here, we are accessing protected members within class by inheritance
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Program : Employee { static void Main(string[] args) { Program program = new Program(); Console.WriteLine($“First Name => {program.FirstName}”); Console.WriteLine($“Last Name => {program.LastName}”); } } class Employee { protected string FirstName = “Robert”; protected string LastName = “White”; protected void MesEmployee(string FirstName, string LastName) { this.FirstName = FirstName; this.LastName = LastName; } } |
Output:
First Name => Robert
Last Name => White
4-Internal
an element defined as internal is accessible in the assembly (Dll or Exe file) in which it is located. There are no restrictions on access inside the dll or Exe file, but it cannot be accessed from the outside.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Program { static void Main(string[] args) { Employee employee = new Employee(); Console.WriteLine($“First Name => {employee.FirstName}”); Console.WriteLine($“Last Name => {employee.LastName}”); } } class Employee { internal string FirstName = “Robert”; internal string LastName = “White”; internal void MesEmployee(string FirstName, string LastName) { this.FirstName = FirstName; this.LastName = LastName; } } |
Output:
First Name => Robert
Last Name => White
5-Protected internal
an element defined as protected internally can be accessed inside the class in which it is defined and inside other classes inheritance from that class. It is also accessible in other classes inheritance from the class it is defined in, even if they are not in the same assembly.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Program { static void Main(string[] args) { Employee employee = new Employee(); Console.WriteLine($“First Name => {employee.FirstName}”); Console.WriteLine($“Last Name => {employee.LastName}”); } } class Employee { protected internal string FirstName = “Robert”; protected internal string LastName = “White”; protected internal void MesEmployee(string FirstName, string LastName) { this.FirstName = FirstName; this.LastName = LastName; } } |
Output:
First Name => Robert
Last Name => White








Bir yanıt yazın