In C#, accessors define the visibility and accessibility of members (fields, properties, methods, etc.) within a class or struct. The main accessors in C# are private, public, and protected. Here’s a detailed explanation of each.
What Are The Accessors
Private Accessor
The members declared with the privateaccessor are accessible only within the same class or struct.
You can use privateto encapsulate data and restrict its access from outside the class. It helps to maintain control over the data and ensures that you can’t modify or access it directly from other parts of the code.
In the example below, the namefield is private and can only be accessed or modified through the SetNameand GetNamemethods.
The convention is also to prefix the field with _.
|
|
Public Accessor
The members declared with the publicaccessor are accessible from any other code. There are no restrictions on their accessibility.
You use publicwhen you want to expose members to be accessible from outside the class or struct, such as in a library or API where you need to provide access to certain functionality.
In the example below, the Nameproperty is public and can be accessed and modified directly from outside the Personclass.
|
|
Protected Accessor
The members declared with the protectedaccessor are accessible within the same class or struct, and in derived classes (subclasses).
You use protectedwhen you want to allow access to members in the base class and any derived classes but restrict access from other parts of the code.
In the example below, the speciesfield is protected and can be accessed in the Animalclass and the Dogclass, which is derived from Animal.
|
|
Additional Access Modifiers
C# also provides other access modifiers like internaland protected internal:
internal makes members accessible within the same assembly, but not from another assembly.
|
|
protected internal makes members accessible within the same assembly and a related class in the assembly.
|
|
What about the implicit access modifiers in the class
You may wonder in the examples above what happens when you don’t specify the access identifier on the class, method or property.
The access modifier of a class itself is important and can affect how the class is accessed. In C#, if a class doesn’t specify an explicit access modifier, it defaults to internal.
Important Notes
- For Top-Level Classes: In C#, top-level classes (i.e., those that aren’t nested within another class) can only have
publicorinternalaccess modifiers. They can’t beprivateorprotected. - For Nested Classes: When a class is nested within another class, it can have any access modifier (
public,private,protected,internal,protected internal).
Example with Nested Classes
|
|
In this example:
InnerPrivateClassis accessible only withinOuterClass.InnerProtectedClassis accessible withinOuterClassand derived classes.InnerInternalClassis accessible within the same assembly.InnerPublicClassis accessible from any assembly.
Conclusion
Understanding and using class access modifiers correctly helps in designing your code’s architecture, controlling access levels, and ensuring proper encapsulation and security.
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.
Photo by Tim Mossholder