Access Modifiers Explained In C#

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.
 
Examples
  1. class Test  
  2. {  
  3. private int id = 1;  
  4. private string name = "Magesh";  
  5. private string city = "Bangalore";  
  6.   
  7. }  
  8. class Program  
  9. {  
  10. static void Main(string[] args)  
  11. {  
  12. Test test = new Test();  
  13. Console.WriteLine("Id:{0}", test.id);  
  14. Console.WriteLine("Name:{0}", test.name);  
  15. 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.  
  16.   
  17. }  
  18.   
  19. }  

Public

 
The public Modifier is all members’ access to all classes and projects. It can be accessed by the object and derived classes.
 
Examples
  1. class Test  
  2. {  
  3. public int id=1;  
  4. public string name= "Magesh";  
  5. public string city= "Bangalore";  
  6.   
  7. }  
  8. class Program  
  9. {  
  10. static void Main(string[] args)  
  11. {  
  12. Test test = new Test();  
  13. Console.WriteLine("Id:{0}", test.id); //Output:1;  
  14. Console.WriteLine("Name:{0}", test.name);//Output:Magesh;  
  15. Console.WriteLine("City:{0}", test.city);// Output:Bangalore;  
  16.   
  17. }  
  18. }  

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.
 
Examples
  1. class Test  
  2. {  
  3. protected int id = 1;  
  4. protected string name = "Magesh";  
  5. protected string city = "Bangalore";//we can access this variables inside this class.  
  6.   
  7. }  
  8. class derivedClass : Test  
  9. {  
  10. void Main()  
  11. {  
  12. Console.WriteLine("Id:{0}", id);//Output :1  
  13. Console.WriteLine("Name:{0}", name);//Output:Magesh  
  14. Console.WriteLine("City:{0}", city);//Output:Bangalore  
  15. }  
  16. }  
  17. class Program3  
  18. {  
  19. static void Main(string[] args)  
  20. {  
  21. Test test3 = new Test();  
  22. Console.WriteLine("Id:{0}", test3.id);  
  23. Console.WriteLine("Name:{0}", test3.name);  
  24. 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 
  25. derive from the Test Class  
  26.   
  27. }  
  28.   
  29. }  

Internal

 
The Internal Modifier allows access only to members in the current project. It can’t be accessed by the object.
 
Examples
  1. //First Project (Assembly)  
  2. class Test  
  3. {  
  4. internal int id = 1;  
  5. internal string name = "Magesh";  
  6. internal string city = "Bangalore";//we can access this variables inside this class.  
  7.   
  8. }  
  9. class Program  
  10. {  
  11. static void Main(string[] args)  
  12. {  
  13. Test test = new Test();  
  14. Console.WriteLine("Id:{0}",test. id);//Output :1  
  15. Console.WriteLine("Name:{0}",test.name);//Output:Magesh  
  16. Console.WriteLine("City:{0}", test.city);//Output:Bangalore  
  17. }  
  18. }  
  19. //second Project(Assembly)  
  20. class Program  
  21. {  
  22. static void Main(string[] args)  
  23. {  
  24.   
  25. Test test = new Test();  
  26. Console.WriteLine("Id:{0}", test.id);  
  27. Console.WriteLine("Name:{0}", test.name);  
  28. Console.WriteLine("City:{0}", test.city);// Error.This Program class is second project we can't access the internal members from another project.  
  29. }  
  30. }  

Protected Internal

 
The protected internal modifier allows access to all members in current projects and all members in derived classes can access the variables.
 
Examples
  1. //First Project (Assembly)  
  2. class Test  
  3. {  
  4. protected internal int id = 1;  
  5. protected internal string name = "Magesh";  
  6. protected internal string city = "Bangalore";//we can access this variables inside this class.  
  7.   
  8. }  
  9. class Program  
  10. {  
  11. static void Main(string[] args)  
  12. {  
  13. Test test = new Test();  
  14. Console.WriteLine("Id:{0}",test. id);//Output :1  
  15. Console.WriteLine("Name:{0}",test.name);//Output:Magesh  
  16. Console.WriteLine("City:{0}", test.city);//Output:Bangalore  
  17. }  
  18. }  
  19.   
  20.   
  21. //second Project(Assembly)  
  22. class Program:Test  
  23. {  
  24. static void Main(string[] args)  
  25. {  
  26. Console.WriteLine("Id:{0}",id );//Output:1;  
  27. Console.WriteLine("Name:{0}",name);//Output:Magesh  
  28. Console.WriteLine("City:{0}",city);//Output:Bangalore  
  29. }  
  30. }  

Private protected

 
The private protected modifier allows access to limited containing class or type derived from the containing class within the current assembly.


Next Recommended Reading Access Modifier In C# With Example