In the c#, Access Modifiers or Access Specifiers are the keywords used to define the visibility of a method or class property
in the C# provides five types of access specifiers like as given below
- Public
- Protected
- Internal
- Protected internal
- Private
The public keyword is an access modifier not restricted, which is used to set the access level/visibility for classes, fields, methods and properties.
In the given table describes about the accessibility of each
Access Specifier | Description |
public | it can accessible for all classes |
private | It can only accessible within the same class |
protected | It can accessible within the same class, or in a class that is inherited from that class. |
protected internal | It specifies that access is limited to the current assembly or types derived from the containing class. |
internal | It can only accessible within its own assembly, but not from another assembly. |
C# Public Access Specifier
using System;
namespace AccessSpecifiers
{
public class PublicExample
{
public string stName = "Sylvia";
public void Msg(string msg)
{
Console.WriteLine("Hello " + msg);
}
}
public class Program
{ public static void Main(string[] args)
{
PublicExample obj = new PublicExample();
// Accessing public variable
Console.WriteLine("Hello " + obj.stName);
// Accessing public function
obj.Msg("Hello FindAndSolve");
}
}
}
Output
Hello Sylvia
Hello Hello FindAndSolve
C# Protected Access Specifier
using System;
namespace AccessSpecifiers
{
class ProtectedExample
{
protected string stName = "Sylvia";
protected void Msg(string msg)
{
Console.WriteLine("Hello " + msg);
}
} public class Program
{ public static void Main(string[] args)
{
ProtectedExample obj = new ProtectedExample();
// Accessing protected variable
Console.WriteLine("Hello "+ obj.stName);
// Accessing protected function
obj.Msg("FindAndSolve");
} }
}
Output
Compilation error (line 18, col 45): 'AccessSpecifiers.ProtectedExample.stName' is inaccessible due to its protection level
Compilation error (line 20, col 17): 'AccessSpecifiers.ProtectedExample.Msg(string)' is inaccessible due to its protection level
using System;
namespace AccessSpecifiers
{
class ProtectedExample
{
protected string stName = "Sylvia";
protected void Msg(string msg) { Console.WriteLine("Hello " + msg);
}
}
class Program : ProtectedExample
{ public static void Main(string[] args)
{
Program program = new Program();
// Accessing protected variable
Console.WriteLine("Hello " + program.stName); // Accessing protected function
program.Msg("Find And Solve");
}
}
}
Output
Hello Sylvia
Hello Hello Find And Solve
C# Internal Access Specifier
In the c# , internal keyword is used to specify the internal access specifier for the variables and functions
using System;
namespace AccessSpecifiers
{
class InternalExample
{
internal string stName= "Sylvia Neupane";
internal void Msg(string msg)
{ Console.WriteLine("Hello " + msg);
}
}
public class Program
{
public static void Main(string[] args)
{
InternalExample obj = new InternalExample();
// Accessing internal variable
Console.WriteLine("Hello " + obj.stName);
// Accessing internal function
obj.Msg("Find And Solve");
}
}
}
Output
Hello Sylvia Neupane
Hello Find And Solve
C# Protected Internal Access Specifier
using System;
namespace AccessSpecifiers {
class InternalExample
{
protected internal string stName = "Sylvia";
protected internal void Msg(string msg) {
Console.WriteLine("Hello " + msg);
}
}
public class Program
{
public static void Main(string[] args)
{
InternalExample obj = new InternalExample(); // Accessing protected internal variable
Console.WriteLine("Hello " + obj.stName);
// Accessing protected internal function
obj.Msg("Find And Solve");
} }
}
Output
Hello Sylvia
Hello Find And Solve
C# Private Access Specifier
using System;
namespace AccessSpecifiers
{
class PrivateExample
{
private string stName = "Sylvia";
private void Msg(string msg)
{
Console.WriteLine("Hello " + msg);
}
}
public class Program
{
public static void Main(string[] args)
{
PrivateExample obj = new PrivateExample();
// Accessing private variable
Console.WriteLine("Hello " + obj.stName);
// Accessing private function
obj.Msg("Find And Solve");
}
}
}
Output
Compilation error (line 18, col 46): 'AccessSpecifiers.PrivateExample.stName' is inaccessible due to its protection level
Compilation error (line 20, col 17): 'AccessSpecifiers.PrivateExample.Msg(string)' is inaccessible due to its protection level
Right Example of Private Access Specifier
using System;
namespace AccessSpecifiers
{
public class Program
{
private string stName = "Sylvia";
private void Msg(string msg)
{ Console.WriteLine("Hello " + msg);
}
public static void Main(string[] args)
{ Program obj = new Program();
// Accessing private variable
Console.WriteLine("Hello " + obj.stName);
// Accessing private function
obj.Msg("Find And Solve");
} }
}
Output
Hello Sylvia
Hello Find And Solve