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 A();  
  40.             Console.WriteLine(a.b);  
  41.             Console.WriteLine(a.d);  
  42.             Console.WriteLine(a.e);  
  43.             Console.WriteLine(b.b);  
  44.             Console.WriteLine(b.d);  
  45.             Console.WriteLine(b.e);  
  46.             Console.WriteLine(c.b);  
  47.             Console.WriteLine(c.d);  
  48.             Console.WriteLine(c.e);  
  49.             b.Display();  
  50.         }  
  51.     }  
  52. }  
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 Display Test 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.

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

Inside class create one internal member. Now add new project console application, add reference of dll in the project and create object of Class1 and try to access internal member it will not be accessible. Because internal members behave as private in different assembly but behave as public within same assembly.

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.     }  
  12. }  
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 Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Class1 c1 = new Class1();  
  13.             c1.a = 10;  
  14.             Console.WriteLine(c1.a);  
  15.         }  
  16.     }  
  17. }  
Thus we can see b is not accessible as it is internal. One more class have only internal and public as access specifier in c sharp and by default class have access specifier as internal.

 

Up Next
    Ebook Download
    View all
    Learn
    View all