Access Specifiers (Access Modifiers) in C#

Access Specifiers (Access Modifiers) are keywords in Object Oriented Programming, that specify accessibility of Types and Types Members.

Remember the following points:

  • By default members of classes are Private.
  • By default classes are Internal.
  • By default namespaces are Public but we are not supposed to specify the public keyword.

Access Specifier are as follows.

1. Private: Members can be accessed within the class only.

Example

  1. namespace Sample  
  2. {  
  3.   
  4.    public class ABC  
  5.    {  
  6.        private int Id;  
  7.    }  
  8.    
  9.     public class Program  
  10.    {  
  11.         public static void Main(string[] args)  
  12.         {  
  13.             ABC abc = new ABC();  
  14.             abc.Id = 10;//Error ,Id is private  
  15.         }  
  16.    }  
  17. }  
Output

Error 1 "Sample.ABC.Id" is inaccessible due to its protection level

2. Public: As the name says, members can be accessed from any class and any assembly.

3. Protected:
Members can be accessed within its class and derived class of the same assembly.

Protected members are also accessible outside the assembly provided it should be derived.

Example 1
  1. namespace Sample  
  2. {  
  3.   
  4.    public class ABC  
  5.    {  
  6.        protected int Id;  
  7.    }  
  8.    
  9.     public class Program  
  10.    {  
  11.         public static void Main(string[] args)  
  12.         {  
  13.             ABC abc = new ABC();  
  14.             abc.Id = 10;//error ,its protected  
  15.         }  
  16.    }  
  17.   
  18. }  
Output

    Error 1 "Sample.ABC.Id" is inaccessible due to its protection level

Example 2

  1. namespace Sample  
  2. {  
  3.   
  4.     public class ABC  
  5.     {  
  6.         protected int Id;  
  7.     }  
  8.     public class XYZ : ABC  
  9.     {  
  10.         public void Print()  
  11.         {  
  12.             Id = 5;  
  13.             Console.WriteLine(Id);  
  14.         }  
  15.     }  
  16.     public class Program  
  17.     {  
  18.         public static void Main(string[] args)  
  19.         {  
  20.             XYZ xyz = new XYZ();  
  21.             xyz.Print();  
  22.             Console.ReadLine();  
  23.         }  
  24.   
  25.     }  
Output

    5

4. Internal: Members can be accessed from anywhere within the same assembly.

Create a Class library
  1. namespace ClassLibrary1  
  2. {  
  3.     public class Class1  
  4.     {  
  5.         internal int a;  
  6.     }  
  7. }  
Now create a console application, take a reference of the preceding library.
  1. namespace Sample  
  2. {   
  3.     public class Program  
  4.    {  
  5.         public static void Main(string[] args)  
  6.         {  
  7.             ClassLibrary1.Class1 cls = new Class1();  
  8.             cls.a = 6; // error             
  9.         }  
  10.    }  
  11. }  
Output

    Error 1 'ClassLibrary1.Class1' does not contain a definition for 'a' and no extension method 'a' accepting a first argument of type 'ClassLibrary1.Class1' could be found (are you missing a using directive or an assembly reference?

5. Protected Internal: Members can be accessed anywhere in the same assembly and also accessible by inheriting that class. It can be accessible outside the assembly in the derived class only. Protected Internal member works as Internal within the same assembly and works as Protected outside the assembly.

  1. namespace ClassLibrary1  
  2. {  
  3.     public class Class1  
  4.     {  
  5.       protected internal int a;  
  6.     }  
  7. }  
Now create a console application and make a reference of the preceding library.
  1. namespace Sample  
  2. {  
  3.   
  4.    public class ABC:Class1  
  5.    {  
  6.        private int Id;  
  7.        public void XYZ()   
  8.        {  
  9.            a = 5;  
  10.            Console.WriteLine(a);  
  11.        }  
  12.    }  
  13.     
  14.     public class Program  
  15.    {  
  16.         public static void Main(string[] args)  
  17.         {  
  18.             ABC abc = new ABC();  
  19.             abc.XYZ();  
  20.             Console.ReadKey();  
  21.              
  22.   
  23.         }  
  24.    }  
  25. }