In this article we are going to discuss Access specifiers in C#. There are many blogs, articles are available on the internet regarding Access specifiers but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Access specifiers use in C#.
Access specifiers Introduction
Access specifiers are keywords to specify the accessibility of a class, method, property, field. The keywords are - Public, Private, Protected, Internal, Protected Internal and PrivateProtected. access specifiers properly helps ensure encapsulation and data hiding, which are key principles of object-oriented programming.
Why use Access specifiers in C#
Types of Access specifiers
C# provides five types of access specifiers.
The below table defines the scope of Access specifiers.
1. Public Access Specifier
It has no limits, any members or types defined as public can be accessed within the class, assembly even outside the assembly. it is accessible for all classes.
Example
class Car
{
public string model = "Maruti Swift";
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.model);
Console.ReadKey();
}
}
Output
2. Private Access Specifier
If you declare a field with a private access modifier, it can only be accessed within the same class. If you try to access it outside the class, an error will occur.
If you declare a field with a private access modifier, it can only be accessed within the same class
class Car
{
private string model = "Maruti Swift";
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.model);
Console.ReadKey();
}
}
Output
3. 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.
If you try to access it outside the class, an error will occur. it is not in the same class and child class.
If you declare a field with a Protected access modifier, it can only be accessed within the same class and child class, in case of inheritance.
class Car
{
protected string model = "Maruti Swift";
}
class Program : Car
{
static void Main(string[] args)
{
Program myObj = new Program();
Console.WriteLine(myObj.model);
Console.ReadKey();
}
}
Output
4. Internal Access Specifier
The internal access specifier in C# is used to limit the accessibility of types and members to the current assembly. This means that any code within the same assembly (i.e., the same compiled .exe or .dll) can access the internal members, but code in a different assembly cannot.
When to Use internal
The internal access specifier is useful when you want to hide implementation details from other assemblies but still need to expose those details within the same assembly for internal use, testing, or other purposes.
Example Usage
Here's a simple example to illustrate the use of the internal access specifier:
// File: Library.cs (in AssemblyA)
namespace MyLibrary
{
internal class InternalClass
{
internal void InternalMethod()
{
Console.WriteLine("This is an internal method.");
}
}
public class PublicClass
{
public void PublicMethod()
{
InternalClass internalClass = new InternalClass();
internalClass.InternalMethod();
}
}
}
In another assembly, you won't be able to access InternalClass or InternalMethod:
// File: Program.cs (in AssemblyB)
using MyLibrary;
class Program
{
static void Main()
{
PublicClass publicClass = new PublicClass();
publicClass.PublicMethod();
// The following lines will cause compilation errors:
// InternalClass internalClass = new InternalClass(); // Error
// internalClass.InternalMethod(); // Error
}
}
5. Protected Internal Access Specifier
The protected internal access specifier in C# combines the behaviors of both protected and internal access specifiers. This means that a member with this access level is accessible from:
Definition
The protected internal access specifier allows a member to be accessed from any class in the same assembly, and from derived classes, even if they are in a different assembly.
Example
class Car
{
protected internal string model = "Maruti Swift";
protected internal void Msg(string msg)
{
Console.WriteLine("Hello " + msg);
}
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
myObj.Msg("Rohatash");
Console.WriteLine(myObj.model);
Console.ReadKey();
}
}
Output