Language: EN

csharp-modificadores-acceso

Access Modifiers in C#

Access modifiers in C# provide control over visibility and access to class and structure members.

Access modifiers are the way to implement encapsulation in C#, one of the pillars of object-oriented programming.

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.

ModifierAccessible from
publicFrom anywhere.
protectedFrom the base class and derived classes.
internalWithin the same assembly.
protected internalFrom the same assembly or from derived classes.
privateOnly 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 an instance of Vehicle is created.

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 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 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.