.Net Framework
C# Tutorial
C# Control Statement
C# Arrays
C# Object Class
C# Properties
C# Inheritance
C# Polymorphism
C# Abstraction
C# Strings
C# Exception Handling
C# File IO
C# Collections
C# Generics
C# Delegates
C# Reflection
Anonymous Function
C# Multithreading
C# Synchronization
C# Web Service
C# Misc
C# New Features
C# Programs
C# Interview Questions
ADO.NET Tutorial
ASP.NET Tutorial
C# Access Modifiers / Specifiers
C# Access Modifiers / Specifiers
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.
- Public
- Protected
- Internal
- Protected internal
- Private
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.
1) C# Public Access Specifier
It makes data accessible publicly. It does not restrict data to the declared block.
Example
namespace AccessSpecifiers
{
class PublicTest
{
public string name = "Anil 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");
}
}
}
Output:
Hello Peter Decosta
2) C# Protected Access Specifier
It is accessible within the class and has limited scope. It is also accessible within sub class or child class, in case of inheritance.
Example
namespace AccessSpecifiers
{
class ProtectedTest
{
protected string name = "srendra kaushik";
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("Nalini");
}
}
}
Output:
'ProtectedTest.name' is inaccessible due to its protection level.
Example2
Here, we are accessing protected members within child class by inheritance.
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("nalini");
}
}
}
Output:
Hello Nalini
3) C# Internal Access Specifier
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.
Example
namespace AccessSpecifiers
{
class InternalTest
{
internal string name = "Sachin ";
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");
}
}
}
Output:
Hello Peter Decosta
4) C# Protected Internal Access Specifier
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.
Example
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");
}
}
}
Output:
Hello Peter Decosta
5) C# Private Access Specifier
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.
Example
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");
}
}
}
Output:
'PrivateTest.name' is inaccessible due to its protection level.
C# Private Specifier Example 2
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");
}
}
}
Output:
Hello Peter Decosta