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. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Test test = new Test();
  12. Console.WriteLine("Id:{0}", test.id);
  13. Console.WriteLine("Name:{0}", test.name);
  14. 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.
  15. }
  16. }

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. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Test test = new Test();
  12. Console.WriteLine("Id:{0}", test.id); //Output:1;
  13. Console.WriteLine("Name:{0}", test.name);//Output:Magesh;
  14. Console.WriteLine("City:{0}", test.city);// Output:Bangalore;
  15. }
  16. }

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

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. 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. //second Project(Assembly)
  19. class Program
  20. {
  21. static void Main(string[] args)
  22. {
  23. Test test = new Test();
  24. Console.WriteLine("Id:{0}", test.id);
  25. Console.WriteLine("Name:{0}", test.name);
  26. Console.WriteLine("City:{0}", test.city);// Error.This Program class is second project we can't access the internal members from another project.
  27. }
  28. }

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. 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. //second Project(Assembly)
  19. class Program:Test
  20. {
  21. static void Main(string[] args)
  22. {
  23. Console.WriteLine("Id:{0}",id );//Output:1;
  24. Console.WriteLine("Name:{0}",name);//Output:Magesh
  25. Console.WriteLine("City:{0}",city);//Output:Bangalore
  26. }
  27. }

Private protected

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