C# Access Modifiers

In C#, access modifiers are keywords used to specify the visibility or accessibility of members (fields, properties, methods, etc.) of a class or struct. They determine where the class members can be accessed from within the code.

1. Types of Access Modifiers

  • public: Accessible from anywhere
  • private: Accessible only within the same class
  • protected: Accessible within the same class and derived classes
  • internal: Accessible within the same assembly (project)
  • protected internal: Accessible within the same assembly and derived classes
  • private protected: Accessible within the same class and derived classes within the same assembly

2. public

Members declared as public can be accessed from any other class, including external assemblies.


class Car
{
    public string make;

    public void ShowCar()
    {
        Console.WriteLine("Car Make: " + make);
    }
}

class Program
{
    static void Main()
    {
        Car car = new Car();
        car.make = "Honda";  // Accessing public field
        car.ShowCar();       // Calling public method
    }
}
    

3. private

Members declared as private can only be accessed within the same class. They are not accessible from outside the class, not even by derived classes.


class Car
{
    private string make; // Private field

    public void SetMake(string m) // Public method to set the private field
    {
        make = m;
    }

    public void ShowCar()
    {
        Console.WriteLine("Car Make: " + make);
    }
}

class Program
{
    static void Main()
    {
        Car car = new Car();
        car.SetMake("Toyota");  // Accessing private field through public method
        car.ShowCar();
    }
}
    

4. protected

Members declared as protected are accessible within the same class and derived classes.


class Car
{
    protected string make;

    public void SetMake(string m)
    {
        make = m;
    }
}

class SportsCar : Car
{
    public void ShowCar()
    {
        Console.WriteLine("Sports Car Make: " + make); // Accessing protected field
    }
}

class Program
{
    static void Main()
    {
        SportsCar sportsCar = new SportsCar();
        sportsCar.SetMake("Ferrari");
        sportsCar.ShowCar();
    }
}
    

5. internal

Members declared as internal are accessible only within the same assembly (i.e., the same project). They cannot be accessed from other assemblies.


class Car
{
    internal string make; // Internal field

    public void ShowCar()
    {
        Console.WriteLine("Car Make: " + make);
    }
}

class Program
{
    static void Main()
    {
        Car car = new Car();
        car.make = "Chevy";  // Accessing internal field within the same assembly
        car.ShowCar();
    }
}
    

6. protected internal

Members declared as protected internal are accessible within the same assembly or from derived classes.


class Car
{
    protected internal string make;

    public void SetMake(string m)
    {
        make = m;
    }
}

class SportsCar : Car
{
    public void ShowCar()
    {
        Console.WriteLine("Sports Car Make: " + make); // Accessing protected internal field
    }
}

class Program
{
    static void Main()
    {
        SportsCar sportsCar = new SportsCar();
        sportsCar.SetMake("Porsche");
        sportsCar.ShowCar();
    }
}
    

7. private protected

Members declared as private protected are accessible within the same class or from derived classes, but only within the same assembly.


class Car
{
    private protected string make;

    public void SetMake(string m)
    {
        make = m;
    }
}

class SportsCar : Car
{
    public void ShowCar()
    {
        Console.WriteLine("Sports Car Make: " + make); // Accessing private protected field
    }
}

class Program
{
    static void Main()
    {
        SportsCar sportsCar = new SportsCar();
        sportsCar.SetMake("Lamborghini");
        sportsCar.ShowCar();
    }
}
    

8. Key Points

  • public: Accessible anywhere
  • private: Accessible only within the same class
  • protected: Accessible within the class and derived classes
  • internal: Accessible within the same assembly
  • protected internal: Accessible within the same assembly and derived classes
  • private protected: Accessible within the same class and derived classes in the same assembly

9. Conclusion

Understanding and using the right access modifiers is essential for encapsulation in object-oriented programming. They help protect the data and control how it can be accessed, ensuring proper use of class members and maintaining the integrity of your code.

C# Introduction C# Variables & Keywords C# Data Types C# If Else Statement C# Loops C# Comments C# Type Casting C# User Input C# Operators C# Math C# String C# OOP Concepts C# Classes and Objects C# Multiple Classes and Objects C# Class Members C# Constructors C# Access Modifiers