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.
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
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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.