Understanding Access Modifiers in C#

Spread the love

Access Modifiers are essential for writing secure, maintainable and organised code. They determine the accessibility of classes, methods, variables, and other members within your code. Access modifiers help control who can see and use certain parts of your code. This blog will explore the different types of access modifiers in C#, how they work, and when to use them in your projects. Access modifiers help maintain encapsulation, one of the fundamental principles of object-oriented programming (OOP).   Now, let’s break down each type of access modifier and understand its role.

Types of Access Modifiers in C#

In C#, there are six access modifiers: public, private, protected, internal, protected internal, and private protected. Each one determines how accessible a class or class member is in relation to other parts of your code.

1. Public: Accessible Everywhere

The public access modifier makes a class or member accessible from anywhere in your program. If you declare a class, method, or variable as public, it can be accessed from any other class, regardless of the project structure.

Example:

public class Employee
{
    public string Name;

    public void PrintName()
    {
        Console.WriteLine(Name);
    }
}

Here, the Employee class and its members (Name and PrintName) are accessible anywhere in the application. You could create a new Employee object in another class and freely access these public members.

When to use:

Use public when you want a class or member to be accessible throughout your project and potentially other projects that reference it.

2. Private: Accessible Only Within the Same Class

The private access modifier is the most restrictive. It makes a class member accessible only within the same class. No other class can directly access or modify a private member.

public class Employee
{
    private string empId;
    
    public void SetID(string id)
    {
        empId= id;
    }

    public string GetID()
    {
        return empId;
    }
}

Here, empId is private, meaning it can’t be accessed outside of the Employee class. You need to use the SetID and GetID methods to modify or retrieve its value.

When to use:

Use private when you want to hide details of a class from outside code. For example, sensitive information (like passwords or IDs) should often be private.

3. Protected: Accessible in Derived Classes

The protected access modifier allows access within the same class and any class that inherits from it (derived classes). This is particularly useful in inheritance scenarios where you want to share certain members between a base class and its subclasses.

public class Person
{
    protected string Name;
}

public class Employee: Person
{

    public void SetName(string name)
    {
        Name = name;
    }

}

Here, the Name field is protected, so it’s accessible within the Person class and the Employee class (which inherits from Person).

When to use:

Use protected when you want to allow access in child classes but not from the outside world. It’s a good choice for properties or methods that are part of a class’s internal structure but needed in its subclasses

4. Internal: Accessible Within the Same Assembly

The internal access modifier allows access only within the same assembly (a compiled .NET project). If you declare something as internal, it won’t be accessible from other assemblies (like other projects in a solution), but any class inside the same assembly can access it.

internal class Employee
{
    public string Designation;
}

Here, the Employee class is internal, so it can only be accessed by other classes in the same project or assembly.

When to use:

Use internal when you want to limit access to code within the same project. It’s helpful when you’re building libraries or modules that shouldn’t expose all their details to other projects.

5. Protected Internal: A Combination of Protected and Internal

The protected internal modifier means the member is accessible within the same assembly or in derived classes even if they are in a different assembly.

public class Manager
{
    protected internal string Department;
}

In this case, the Department is accessible within the same assembly and in derived classes, regardless of the assembly they belong to.

When to use:

This modifier is useful when you want to provide broader access than protected or internal alone but still keep things somewhat restricted.

6. Private Protected: Limited Access to Derived Classes in the Same Assembly

The private protected access modifier restricts access to derived classes but only within the same assembly. This means that a derived class in another assembly won’t have access to the member.

public class Company
{
    private protected string Name;
}

In this case, only derived classes within the same assembly as the Company can access the Name field.

When to use:

Use private protected when you want to tightly control access, allowing derived classes access but only within the same assembly. It’s rarely used but can be useful in complex project structures.

Summary: Benefits of Using Access Modifiers

Access modifiers are essential for:

  • Encapsulation: Keeping your code secure by hiding unnecessary details.
  • Code Maintenance: Make your code more readable and organised by restricting access where it’s not needed.
  • Security: Protecting sensitive data from being accessed or modified from external classes.
  • Flexibility: Providing the right level of access based on project requirements.


Spread the love