C# Access modifiers or specifiers are the keywords that are used to specify accessibility or scope of variables and functions in the C# application.
C# provides five types of access specifiers.
We can choose any of these to protect our data. Public is not restricted and Private is most restricted. The following table describes about the accessibility of each.
Access Specifier | Description |
---|---|
Public | It specifies that access is not restricted. |
Protected | It specifies that access is limited to the containing class or in derived class. |
Internal | It specifies that access is limited to the current assembly. |
protected internal | It specifies that access is limited to the current assembly or types derived from the containing class. |
Private | It specifies that access is limited to the containing type. |
Now, let's create examples to check accessibility of each access specifier.
It makes data accessible publicly. It does not restrict data to the declared block.
using System; namespace AccessSpecifiers { class PublicTest { public string name = "Shantosh Kumar"; public void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { PublicTest publicTest = new PublicTest(); // Accessing public variable Console.WriteLine("Hello " + publicTest.name); // Accessing public function publicTest.Msg("Peter Decosta"); } } }
It is accessible within the class and has limited scope. It is also accessible within sub class or child class, in case of inheritance.
using System; namespace AccessSpecifiers { class ProtectedTest { protected string name = "Shashikant"; protected void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { ProtectedTest protectedTest = new ProtectedTest(); // Accessing protected variable Console.WriteLine("Hello "+ protectedTest.name); // Accessing protected function protectedTest.Msg("Swami Ayyer"); } } }
Here, we are accessing protected members within child class by inheritance.
using System; namespace AccessSpecifiers { class ProtectedTest { protected string name = "Shashikant"; protected void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program : ProtectedTest { static void Main(string[] args) { Program program = new Program(); // Accessing protected variable Console.WriteLine("Hello " + program.name); // Accessing protected function program.Msg("Swami Ayyer"); } } }
The internal keyword is used to specify the internal access specifier for the variables and functions. This specifier is accessible only within files in the same assembly.
using System; namespace AccessSpecifiers { class InternalTest { internal string name = "Shantosh Kumar"; internal void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { InternalTest internalTest = new InternalTest(); // Accessing internal variable Console.WriteLine("Hello " + internalTest.name); // Accessing internal function internalTest.Msg("Peter Decosta"); } } }
Variable or function declared protected internal can be accessed in the assembly in which it is declared. It can also be accessed within a derived class in another assembly.
using System; namespace AccessSpecifiers { class InternalTest { protected internal string name = "Shantosh Kumar"; protected internal void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { InternalTest internalTest = new InternalTest(); // Accessing protected internal variable Console.WriteLine("Hello " + internalTest.name); // Accessing protected internal function internalTest.Msg("Peter Decosta"); } } }
Private Access Specifier is used to specify private accessibility to the variable or function. It is most restrictive and accessible only within the body of class in which it is declared.
using System; namespace AccessSpecifiers { class PrivateTest { private string name = "Shantosh Kumar"; private void Msg(string msg) { Console.WriteLine("Hello " + msg); } } class Program { static void Main(string[] args) { PrivateTest privateTest = new PrivateTest(); // Accessing private variable Console.WriteLine("Hello " + privateTest.name); // Accessing private function privateTest.Msg("Peter Decosta"); } } }
using System; namespace AccessSpecifiers { class Program { private string name = "Shantosh Kumar"; private void Msg(string msg) { Console.WriteLine("Hello " + msg); } static void Main(string[] args) { Program program = new Program(); // Accessing private variable Console.WriteLine("Hello " + program.name); // Accessing private function program.Msg("Peter Decosta"); } } }