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
- 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);
-
- }
-
- }
Public
The public Modifier is all members’ access to all classes and projects. It can be accessed by the object and derived classes.
Examples
- 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);
- Console.WriteLine("Name:{0}", test.name);
- Console.WriteLine("City:{0}", test.city);
-
- }
- }
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
- class Test
- {
- protected int id = 1;
- protected string name = "Magesh";
- protected string city = "Bangalore";
-
- }
- class derivedClass : Test
- {
- void Main()
- {
- Console.WriteLine("Id:{0}", id);
- Console.WriteLine("Name:{0}", name);
- Console.WriteLine("City:{0}", city);
- }
- }
- 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);
-
-
- }
-
- }
Internal
The Internal Modifier allows access only to members in the current project. It can’t be accessed by the object.
Examples
-
- class Test
- {
- internal int id = 1;
- internal string name = "Magesh";
- internal 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);
- }
- }
-
- 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);
- }
- }
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
-
- class Test
- {
- protected internal int id = 1;
- protected internal string name = "Magesh";
- protected internal 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);
- }
- }
-
-
-
- class Program:Test
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Id:{0}",id );
- Console.WriteLine("Name:{0}",name);
- Console.WriteLine("City:{0}",city);
- }
- }
Private protected
The private protected modifier allows access to limited containing class or type derived from the containing class within the current assembly.