Access Specifiers In C#

Here are the access specifiers, 

Access Specifiers
  1. Private
    Private Members of class are accessible only within the class containing the members.

  2. Public
    Public Members of class are accessible any where outside the class containing the members and also within the class containing the members.

  3. Protected
    It is the combination of Private and Public access specifiers.

  4. Internal
    Within the same assembly they will behave as public, but within the different assembly they will behave as Private.

  5. Protected Internal
    It is the combination of Protected and Internal Access Specifiers.

This is the theoretical explanation of all access specifier, now let us see it practically. So you will understand the exact difference. Open Visual Studio and create a Console Application. Here is the program,

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace AccessSpecifiers  
  6. {  
  7.     class A  
  8.     {  
  9.         private int a = 10;  
  10.         public int b = 20;  
  11.         protected int c = 30;  
  12.         internal int d = 40;  
  13.         protected internal int e = 50;  
  14.         public void DisplayTest()  
  15.         {  
  16.             Console.WriteLine(a);  
  17.             Console.WriteLine(b);  
  18.             Console.WriteLine(c);  
  19.             Console.WriteLine(d);  
  20.             Console.WriteLine(e);  
  21.         }  
  22.     }  
  23.     class B: A  
  24.     {  
  25.         public void Display()  
  26.         {  
  27.             Console.WriteLine(b);  
  28.             Console.WriteLine(c);  
  29.             Console.WriteLine(d);  
  30.             Console.WriteLine(e);  
  31.         }  
  32.     }  
  33.     class Program  
  34.     {  
  35.         static void Main(string[] args)  
  36.         {  
  37.             A a = new A();  
  38.             B b = new B();  
  39.             A c = new B();  
  40.             Console.WriteLine(a.b);  
  41.             Console.WriteLine(a.d);  
  42.             Console.WriteLine(a.e);
  43.   
  44.             Console.WriteLine(b.b);  
  45.             Console.WriteLine(b.d);  
  46.             Console.WriteLine(b.e);  

  47.             Console.WriteLine(c.b);  
  48.             Console.WriteLine(c.d);  
  49.             Console.WriteLine(c.e);  
  50.             b.Display();  
  51.         }  
  52.     }  
  53. }  
There are 5 members in class A, a which is private, b is public, c is protected, d is internal, e is protected internal.

Now, I have created method in class A name is DisplayTest which is public. I have created one more Class B which inherits A, in that there is one method Display which is public. Now you can see b, c, d and e can be accessed inside method in class B. But 'a' can not be accessed because it is private and its scope is limited up to Class A and not outside class A.

Now look at main method inside that I have created objects of class A and B. Using these objects we can access only b, d and e, but not c as it is protected.

Thus we can understand the difference between protected and private and why it is said that protected access specifier scope is between public and private. Private members are accessible only within the class containing the members and protected members are accessible within the class containing the members and classes derived from it.

Now about internal they behave as public within the same assembly but behave as private within another assembly.
 
And Protected Internal is the combination of Protected and Internal.Lets understand this 

Open Visual Studio and create a class library i.e. dll file.

Inside this class library add one internal variable,a public variable and a protected internal variable. Now create new console application project,add reference of this class library in the project and create a class,name it as Class2 and derive Class1 which in class library from this Class2 class.Try to access internal member inside this Class2 class it will not be accessible,but protected internal member will be accessible.Because internal access specifiers behave as private in different assembly but behave as public within same assembly.
 
And protected internal is the combination of protected and internal,within different assembly it behaves as protected that is it will be accessible within the class and derived class,but within same assembly it behaves as internal,that is it will be accessible within the class and any where outside the class,same like public as internal within same assembly works like public. 

This is the code for class library name test
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Test  
  6. {  
  7.     public class Class1  
  8.     { 
  9.         public int a = 0; 
  10.         internal int b = 0; 
  11.         protected internal int c=0;   
  12.     }  
  13. }  
And following is the code for console application.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Test;  
  6. namespace ConsoleApplication1  
  7. {  
  8.     class Class2 : Class1
  9.     {
  10.          public void Display()
  11.          {
  12.             Console.WriteLine(a);
  13.             Console.WriteLine(c);
  14.          }
  15.     }
  16.     class Program  
  17.     {  
  18.         static void Main(string[] args)  
  19.         {  
  20.             Class1 c1 = new Class1();  
  21.             c1.a = 10;  
  22.             Console.WriteLine(c1.a);  
  23.         }  
  24.     }  
  25. }  
Thus we can see b is not accessible as it is internal.c is protected internal it is accessible only within derived class and a is public it can be accessible any where.One more class have only internal and public as access specifier in c sharp and by default class have access specifier as internal.