Here are the access specifiers,
Access Specifiers
- Private
Private Members of class are accessible only within the class containing the members.
- Public
Public Members of class are accessible any where outside the class containing the members and also within the class containing the members.
- Protected
It is the combination of Private and Public access specifiers.
- Internal
Within the same assembly they will behave as public, but within the different assembly they will behave as Private.
- 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,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace AccessSpecifiers
- {
- class A
- {
- private int a = 10;
- public int b = 20;
- protected int c = 30;
- internal int d = 40;
- protected internal int e = 50;
- public void DisplayTest()
- {
- Console.WriteLine(a);
- Console.WriteLine(b);
- Console.WriteLine(c);
- Console.WriteLine(d);
- Console.WriteLine(e);
- }
- }
- class B: A
- {
- public void Display()
- {
- Console.WriteLine(b);
- Console.WriteLine(c);
- Console.WriteLine(d);
- Console.WriteLine(e);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- A a = new A();
- B b = new B();
- A c = new B();
- Console.WriteLine(a.b);
- Console.WriteLine(a.d);
- Console.WriteLine(a.e);
-
- Console.WriteLine(b.b);
- Console.WriteLine(b.d);
- Console.WriteLine(b.e);
- Console.WriteLine(c.b);
- Console.WriteLine(c.d);
- Console.WriteLine(c.e);
- b.Display();
- }
- }
- }
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Test
- {
- public class Class1
- {
- public int a = 0;
- internal int b = 0;
- protected internal int c=0;
- }
- }
And following is the code for console application.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Test;
- namespace ConsoleApplication1
- {
- class Class2 : Class1
- {
- public void Display()
- {
- Console.WriteLine(a);
- Console.WriteLine(c);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Class1 c1 = new Class1();
- c1.a = 10;
- Console.WriteLine(c1.a);
- }
- }
- }
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.