Language: EN

csharp-modificadores-acceso

Access Modifiers in C#

Access modifiers provide control over the visibility and mutability of class and structure members.

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 out the Object-Oriented Programming Course leer más ⯈

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.

  • public: accessible from anywhere.
  • protected: accessible from the base class and derived classes.
  • private: accessible only within the base class.
  • internal: accessible within the same assembly.
  • protected internal: accessible from the same assembly or from derived classes.

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 Brand and Model properties, as well as the Start method, are public and can be accessed from anywhere an object of Vehicle is instantiated.

Private Modifier

The private modifier restricts access to the 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 Deposit and GetBalance methods.

Internal Modifier

The internal modifier restricts access to the 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 InternalService class and its Execute method are only accessible within the same assembly.

Protected Modifier

The protected modifier allows members to be accessible within its class and by any derived class. This is useful for defining members that need to 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(); // Allowed access to protected method of the base class
        Console.WriteLine("The dog is barking.");
    }
}

In this example, the Eat method is protected and can be called by the derived Dog class, but is not available outside of this inheritance hierarchy.

Protected Internal Modifier

internal and protected can be combined 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(); // Allowed access
    }
}

In this example, Method can be accessed by both derived classes and any class in the same assembly.