Access modifiers are used to specify the scope of accessibility of a member of a class or type of the class itself. There are six different types of access modifiers.
Public
Private
Protected
Internal
Protected Internal
Private Protected
Objects that implement public access modifiers are accessible from everywhere in a project without any restrictions.
using System; namespace MyApplication{ public class Program{ public static void Main(){ Person person = new Person(); Console.WriteLine(person.Name); //Person Name is accessible as it is public } } public class Person{ public string Name = "Mark"; } }
Objects that implement private access modifier are accessible only inside a class or a structure. As a result, we can’t access them outside the class they are created.
using System; namespace MyApplication{ public class Program{ public static void Main(){ Person person = new Person(); Console.WriteLine(person.Name); //Since Name is private it is not accessible in Program class. // Error: Person.Name is inaccessible due to its protection level. } } public class Person{ private string Name = "Mark"; } }
The protected keyword implies that the object is accessible inside the class and in all classes that derive from that class.
using System; namespace MyApplication{ public class Program{ public static void Main(){ Employee employee = new Employee(); employee.Print(); //Output: Mark Person person = new Person(); Console.WriteLine(person.Name); // Error: Person.Name is inaccessible due to its protection level. } } public class Person{ protected string Name = "Mark"; } public class Employee : Person{ public void Print(){ Console.WriteLine(Name); } } }
For Internal keyword, the access is limited exclusively to classes defined within the current project assembly.
Project 1 −
using System; namespace MyApplication{ public class Program{ public static void Main(){ Person person = new Person(); Console.WriteLine(person.Name); //Output: Mark } } public class Person{ internal string Name = "Mark"; } }
Mark
Project 2 −
using MyApplication; using System; namespace Project2{ public class Project2Class{ public void Print(){ Person person = new Person(); Console.WriteLine(person.Name); // Error: Person.Name is inaccessible due to its protection level. } } }
The protected internal access modifier is a combination of protected and internal. As a result, we can access the protected internal member only in the same assembly or in a derived class in other assemblies.
Project 1 −
using System; namespace MyApplication{ public class Program{ public static void Main(){ Person person = new Person(); Console.WriteLine(person.Name); //Output: Mark } } public class Person{ protected internal string Name = "Mark"; } }
Mark
Project 2 −
using MyApplication; using System; namespace Project2{ public class Project2Class : Person{ public void Print(){ Console.WriteLine(Name); //Output: Mark } } }
The private protected access modifier is a combination of the private and protected keywords. We can access members inside the containing class or in a class that derives from a containing class, but only in the same assembly(project). Therefore, if we try to access it from another assembly, we will get an error. This modifier is valid in C# version 7.2 and later.