Access modifiers in C# provide control over visibility and access to the members of classes and structures.
Access modifiers are the way to implement encapsulation in C#, one of the pillars of object-oriented programming.
If you want to learn more about Encapsulation
check the Object-Oriented Programming Course read more
Access Modifiers
Members of the base class can have different levels of accessibility: public
, protected
, internal
, protected internal
, and private
.
The accessibility of these members determines how they can be used by the derived class.
Modifier | Accessible from |
---|---|
public | From anywhere. |
protected | From the base class and derived classes. |
internal | Within the same assembly. |
protected internal | From the same assembly or derived classes. |
private | Only within the class. |
Public Modifier
The public modifier allows members to be accessible from anywhere in the code, without restrictions. It is useful for defining the public API of a class, allowing other components to interact with it.
public class Vehicle
{
public string Brand { get; set; }
public string Model { get; set; }
public void Start()
{
Console.WriteLine($"{Brand} {Model} has started.");
}
}
Here, the properties Brand
and Model
, as well as the method Start
, are public and can be accessed from anywhere a Vehicle
object is instantiated.
Private Modifier
The private modifier restricts access to members only to the class or structure that defines them. This means that no other code outside of this class can access these members, promoting encapsulation and protection of internal data.
public class BankAccount
{
private decimal balance;
public BankAccount(decimal initialBalance)
{
balance = initialBalance;
}
public void Deposit(decimal amount)
{
balance += amount;
}
public decimal GetBalance()
{
return balance;
}
}
In this example, balance
is declared as private
, so it can only be accessed and modified through the public methods Deposit
and GetBalance
.
Internal Modifier
The internal modifier restricts access to members to any code within the same assembly (i.e., the same project or library). This is useful for components that need to be accessible throughout the application, but not publicly exposed to other assemblies.
internal class InternalService
{
internal void Execute()
{
Console.WriteLine("Executing internal service.");
}
}
In this example, the class InternalService
and its method Execute
are only accessible within the same assembly.
Protected Modifier
The protected modifier allows members to be accessible within their class and by any derived class. This is useful for defining members that should be accessible for inheritance but not publicly exposed.
public class Animal
{
protected void Eat()
{
Console.WriteLine("The animal is eating.");
}
}
public class Dog : Animal
{
public void Bark()
{
Eat(); // Access allowed to the protected method of the base class
Console.WriteLine("The dog is barking.");
}
}
In this example, the method Eat
is protected
and can be called by the derived class Dog
, but it is not available outside this inheritance hierarchy.
Protected Internal Modifier
You can combine internal and protected to allow access within the same assembly and in derived classes.
public class Base
{
protected internal void Method()
{
Console.WriteLine("Method accessible from the same assembly or derived classes.");
}
}
public class Derived : Base
{
public void CallMethod()
{
Method(); // Access allowed
}
}
In this example, Method
can be accessed by both derived classes and any class in the same assembly.