Access Modifiers
In this blog, I will explain the Access Modifiers in C#. The access modifiers are keywords used to access object and class members in the C# project. The following access modifiers are used in C#.
- Private
- Public
- Protected
- Internal
- Protected Internal
- Private protected
Accessibility Levels
Private
The private modifier accesses class members only. It can’t be accessed by the object and derived classes.
- class Test
- {
- private int id = 1;
- private string name = "Magesh";
- private string city = "Bangalore";
- }
- class Program
- {
- static void Main(string[] args)
- {
- Test test = new Test();
- Console.WriteLine("Id:{0}", test.id);
- Console.WriteLine("Name:{0}", test.name);
- Console.WriteLine("City:{0}", test.city); // Error. We can't access the test class variables because it has the private access modifier and its only accessible test class.
- }
- }
Public
The public Modifier is all members’ access to all classes and projects. It can be accessed by the object and derived classes.
- class Test
- {
- public int id=1;
- public string name= "Magesh";
- public string city= "Bangalore";
- }
- class Program
- {
- static void Main(string[] args)
- {
- Test test = new Test();
- Console.WriteLine("Id:{0}", test.id); //Output:1;
- Console.WriteLine("Name:{0}", test.name);//Output:Magesh;
- Console.WriteLine("City:{0}", test.city);// Output:Bangalore;
- }
- }
Protected
The protected Modifier allows access to all members in current class and derived classes can access the variables. It can’t be accessed by the object.
- class Test
- {
- protected int id = 1;
- protected string name = "Magesh";
- protected string city = "Bangalore";//we can access this variables inside this class.
- }
- class derivedClass : Test
- {
- void Main()
- {
- Console.WriteLine("Id:{0}", id);//Output :1
- Console.WriteLine("Name:{0}", name);//Output:Magesh
- Console.WriteLine("City:{0}", city);//Output:Bangalore
- }
- }
- class Program3
- {
- static void Main(string[] args)
- {
- Test test3 = new Test();
- Console.WriteLine("Id:{0}", test3.id);
- Console.WriteLine("Name:{0}", test3.name);
- Console.WriteLine("City:{0}", test3.city);// Error. We can't access the test class variables because it has the protected access modifier.The Program class doesn't
- derive from the Test Class
- }
- }
Internal
The Internal Modifier allows access only to members in the current project. It can’t be accessed by the object.
- //First Project (Assembly)
- class Test
- {
- internal int id = 1;
- internal string name = "Magesh";
- internal string city = "Bangalore";//we can access this variables inside this class.
- }
- class Program
- {
- static void Main(string[] args)
- {
- Test test = new Test();
- Console.WriteLine("Id:{0}",test. id);//Output :1
- Console.WriteLine("Name:{0}",test.name);//Output:Magesh
- Console.WriteLine("City:{0}", test.city);//Output:Bangalore
- }
- }
- //second Project(Assembly)
- class Program
- {
- static void Main(string[] args)
- {
- Test test = new Test();
- Console.WriteLine("Id:{0}", test.id);
- Console.WriteLine("Name:{0}", test.name);
- Console.WriteLine("City:{0}", test.city);// Error.This Program class is second project we can't access the internal members from another project.
- }
- }
Protected Internal
The protected internal modifier allows access to all members in current projects and all members in derived classes can access the variables.
- //First Project (Assembly)
- class Test
- {
- protected internal int id = 1;
- protected internal string name = "Magesh";
- protected internal string city = "Bangalore";//we can access this variables inside this class.
- }
- class Program
- {
- static void Main(string[] args)
- {
- Test test = new Test();
- Console.WriteLine("Id:{0}",test. id);//Output :1
- Console.WriteLine("Name:{0}",test.name);//Output:Magesh
- Console.WriteLine("City:{0}", test.city);//Output:Bangalore
- }
- }
- //second Project(Assembly)
- class Program:Test
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Id:{0}",id );//Output:1;
- Console.WriteLine("Name:{0}",name);//Output:Magesh
- Console.WriteLine("City:{0}",city);//Output:Bangalore
- }
- }
Private protected
The private protected modifier allows access to limited containing class or type derived from the containing class within the current assembly.

Logesh PalaniPosted Dec 6, 2019, 1:01 PM
You should give example for better understanding
Upendra Pratap ShahiPosted Dec 6, 2019, 7:10 AM
Nice informative article
Sourav Kumar DasPosted Dec 4, 2019, 11:17 PM
Nice informative article Sir.