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#:
- public
- private
- protected
- internal
- protected internal
- 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:
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:
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:
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:
5. protected internal
- Description: The
protected internal
access modifier combines the behaviors of bothprotected
andinternal
. A member markedprotected internal
is accessible within the same assembly (likeinternal
) and in derived classes (likeprotected
), 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:
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 ofprivate
andprotected
, 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:
Summary of Access Modifiers:
Modifier | Same Class | Derived Class | Same Assembly | Other Assemblies |
---|---|---|---|---|
public | Yes | Yes | Yes | Yes |
private | Yes | No | No | No |
protected | Yes | Yes | No | No |
internal | Yes | Yes | Yes | No |
protected internal | Yes | Yes | Yes | Yes (only in derived classes) |
private protected | Yes | Yes | Yes | No |
Key Points to Remember:
- 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.
- Visibility: Always choose the least permissive access level that still allows the functionality you need. For example, use
private
whenever possible, andpublic
only when necessary. - Inheritance: Modifiers like
protected
andprivate 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#.