C# Access Modifiers


Access modifiers in C# are keywords used to define the visibility and accessibility of classes, methods, fields, properties, and other members within your code. They determine how these members are accessed in different parts of your application, providing control over how data is exposed to other parts of the program.

List of Access Modifiers in C#:

  1. public
  2. private
  3. protected
  4. internal
  5. protected internal
  6. private protected

1. public

  • Description: The public access modifier makes a class, method, or member accessible from any other class or code in the same assembly or other assemblies (projects). This means the member is fully open for use in the entire application.
  • Usage: Used when you want to allow full access to a member or class.

Example:

public class Person { public string Name; // Can be accessed anywhere }
Person p = new Person(); p.Name = "John"; // Can be accessed and modified from outside the class

2. private

  • Description: The private access modifier restricts access to the class, method, or member within the same class only. It is the most restrictive access level and is used to hide implementation details from outside code.
  • Usage: Used to hide fields or methods that should not be accessed directly from outside the class.

Example:

public class Person { private string Name; // Only accessible within the Person class public void SetName(string name) { Name = name; // Accessed inside the class } }
Person p = new Person(); // p.Name = "John"; // This will cause an error since Name is private p.SetName("John"); // Allowed through a public method

3. protected

  • Description: The protected access modifier allows access to a member within the same class or derived classes (subclasses). It prevents access from outside the class, but any class that inherits from this class can access the member.
  • Usage: Used when you want to allow access to members by derived classes but keep them hidden from the outside world.

Example:

public class Person { protected string Name; // Accessible within the class or derived classes } public class Employee : Person { public void Display() { Name = "John"; // Allowed since Employee inherits from Person } }

4. internal

  • Description: The internal access modifier allows access to a member only within the same assembly (project). It is accessible to all classes in the same project but not from other projects.
  • Usage: Used to limit access to within the same assembly without making it public.

Example:

internal class Person { internal string Name; // Accessible within the same assembly }
Person p = new Person(); p.Name = "John"; // Allowed within the same project

5. protected internal

  • Description: The protected internal access modifier combines the behaviors of both protected and internal. A member marked protected internal is accessible within the same assembly (like internal) and in derived classes (like protected), even if the derived class is in a different assembly.
  • Usage: Used when you want to allow access to members from the same assembly or any derived class in other assemblies.

Example:

public class Person { protected internal string Name; // Accessible within the same assembly and derived classes }
Person p = new Person(); p.Name = "John"; // Allowed within the same assembly

6. private protected

  • Description: The private protected access modifier allows access only within the class or derived classes within the same assembly. It's a combination of private and protected, meaning the member is accessible by derived classes but only if they are in the same assembly.
  • Usage: Used to further restrict access to derived classes within the same assembly while preventing access from other assemblies.

Example:

public class Person { private protected string Name; // Accessible within the class or derived classes within the same assembly }
class Employee : Person { public void Display() { Name = "John"; // Allowed since Employee is in the same assembly and derives from Person } }

Summary of Access Modifiers:

ModifierSame ClassDerived ClassSame AssemblyOther Assemblies
publicYesYesYesYes
privateYesNoNoNo
protectedYesYesNoNo
internalYesYesYesNo
protected internalYesYesYesYes (only in derived classes)
private protectedYesYesYesNo

Key Points to Remember:

  1. Encapsulation: Access modifiers are an essential part of encapsulation in object-oriented programming. They allow you to control which parts of your program can access certain members.
  2. Visibility: Always choose the least permissive access level that still allows the functionality you need. For example, use private whenever possible, and public only when necessary.
  3. Inheritance: Modifiers like protected and private protected help control how members are inherited in child classes, ensuring a proper balance of visibility for subclasses.

Access modifiers are a key aspect of designing secure, maintainable, and modular code in C#.