techmore.in

C# - Access Modifiers

Access modifiers in C# determine the visibility and accessibility of classes, methods, and other members. C# provides several access modifiers to control how members of a class are accessed from different parts of a program. The access modifiers are:

1. Public

  • Visibility: Accessible from anywhere, both inside and outside of the class or assembly.
  • Usage: Use public when you want a class, method, property, or field to be accessible from any other class or assembly.
csharp
public class Car { public string Make { get; set; } // Accessible from anywhere }

In the above example, the Make property of the Car class is public, meaning it can be accessed from any other class or assembly.


2. Private

  • Visibility: Accessible only within the class where it is declared.
  • Usage: Use private when you want to restrict access to members within the same class. By default, members of a class are private if no modifier is specified.
csharp
public class Car { private string engineNumber; // Accessible only within the Car class public void SetEngineNumber(string number) { engineNumber = number; } public string GetEngineNumber() { return engineNumber; } }

Here, engineNumber is a private field and can only be accessed within the Car class. Methods like SetEngineNumber and GetEngineNumber are used to manipulate the private field.


3. Protected

  • Visibility: Accessible within the class where it is declared and by derived classes (inheritance).
  • Usage: Use protected when you want a member to be accessible only within its class and any subclass (inherited class).
csharp
public class Animal { protected void Breathe() { Console.WriteLine("Animal is breathing."); } } public class Dog : Animal { public void Bark() { Breathe(); // Accessible in derived class Console.WriteLine("Dog is barking."); } }

In this case, the Breathe method is marked as protected, meaning it can be called in the derived class Dog but not from an object of Animal or Dog.


4. Internal

  • Visibility: Accessible only within the same assembly (project). It is not accessible from outside the assembly.
  • Usage: Use internal when you want to restrict access to members within the same assembly but allow other classes in the assembly to access them.
csharp
internal class Car { internal string Model { get; set; } // Accessible within the same assembly }

The Car class and its Model property are marked as internal. This means they can only be accessed by classes that are within the same assembly.


5. Protected Internal

  • Visibility: Accessible within the same assembly or in derived classes (subclasses) in other assemblies.
  • Usage: Use protected internal when you want a member to be accessible within the same assembly or from derived classes even if they are in different assemblies.
csharp
public class Car { protected internal string LicenseNumber { get; set; } // Accessible in derived classes or within the assembly }

In this example, LicenseNumber can be accessed from any class in the same assembly or from any derived class, even in another assembly.


6. Private Protected

  • Visibility: Accessible only within the containing class or in derived classes within the same assembly.
  • Usage: Use private protected when you want a member to be accessible within the same class or derived classes but only within the same assembly. It is the most restrictive access modifier combining protected and internal.
csharp
public class Animal { private protected void Move() { Console.WriteLine("Animal is moving."); } } public class Dog : Animal { public void Run() { Move(); // Accessible because it's within the same assembly Console.WriteLine("Dog is running."); } }

Here, the Move method is private protected, meaning it can only be accessed within the same assembly by derived classes of Animal.


Access Modifier Summary Table

ModifierSame ClassDerived Class (Same Assembly)Derived Class (Different Assembly)Same AssemblyDifferent Assembly
publicYesYesYesYesYes
privateYesNoNoNoNo
protectedYesYesYesNoNo
internalYesYesNoYesNo
protected internalYesYesYesYesNo
private protectedYesYesNoNoNo

Examples of Usage

Public Example:

csharp
public class Example { public int PublicProperty { get; set; } // Accessible anywhere } Example obj = new Example(); obj.PublicProperty = 5; // Accessible from anywhere

Private Example:

csharp
public class Example { private int PrivateField; // Accessible only within this class public void SetField(int value) { PrivateField = value; // Modify the private field using a public method } }

Protected Example:

csharp
public class BaseClass { protected void Display() // Accessible in derived classes { Console.WriteLine("Protected method."); } } public class DerivedClass : BaseClass { public void Show() { Display(); // Accessing protected method from base class } }

Internal Example:

csharp
internal class Example { internal void Display()