Access Modifiers in C#
What are Access Modifiers?
Access modifiers in C# are keywords used to define the visibility and accessibility of classes, methods, variables, and other members. These modifiers control how objects interact and ensure encapsulation and proper design.
Types of Access Modifiers
C# supports five main access modifiers: private, public, protected, internal, and protected internal. Each serves a unique purpose in controlling access within and across classes and assemblies.
Modifier | Accessibility |
---|---|
private |
Accessible only within the same class. |
public |
Accessible from any other class or assembly. |
protected |
Accessible within its class and by derived class instances. |
internal |
Accessible only within the same assembly. |
protected internal |
Accessible within the same assembly or from derived classes. |
Example: Access Modifiers in C#
Below is an example that demonstrates how different access modifiers work in practice within a class hierarchy.
Example Code:
public class BaseClass
{
private int privateValue = 10; // Accessible only within BaseClass
public int publicValue = 20; // Accessible from anywhere
protected int protectedValue = 30; // Accessible from BaseClass and derived classes
internal int internalValue = 40; // Accessible within the same assembly
protected internal int protectedInternalValue = 50; // Accessible within assembly or derived classes
public void DisplayValues()
{
Console.WriteLine($"Private: {privateValue}, Public: {publicValue}, Protected: {protectedValue}, Internal: {internalValue}, Protected Internal: {protectedInternalValue}");
}
}
public class DerivedClass : BaseClass
{
public void DisplayDerivedValues()
{
// Cannot access privateValue (only in BaseClass)
Console.WriteLine($"Public: {publicValue}, Protected: {protectedValue}, Internal: {internalValue}, Protected Internal: {protectedInternalValue}");
}
}
// Usage
BaseClass baseObj = new BaseClass();
baseObj.DisplayValues(); // Accessible: public, internal, protected internal
DerivedClass derivedObj = new DerivedClass();
derivedObj.DisplayDerivedValues(); // Accessible: public, protected, internal, protected internal
In this example, the BaseClass
defines fields with various access modifiers. The DerivedClass
can access all except the private
member. The internal
and protected internal
members are accessible within the same assembly.
The new
Keyword
The new
keyword in C# is used for two purposes: creating objects and hiding inherited members. When applied to methods or properties, it hides the base class implementation, allowing the derived class to define its own version.
Example of new
Keyword:
public class BaseClass
{
public void Display()
{
Console.WriteLine("BaseClass Display");
}
}
public class DerivedClass : BaseClass
{
// Hides the BaseClass method
public new void Display()
{
Console.WriteLine("DerivedClass Display");
}
}
// Usage
BaseClass baseObj = new BaseClass();
baseObj.Display(); // Output: BaseClass Display
DerivedClass derivedObj = new DerivedClass();
derivedObj.Display(); // Output: DerivedClass Display
In this example, the DerivedClass
hides the Display
method of BaseClass
using the new
keyword. The base class method is still accessible when using a BaseClass
reference.
Key Points to Remember
- The
private
modifier restricts access to within the class only. - The
public
modifier allows access from anywhere. - The
protected
modifier allows access within the class and its derived classes. - The
internal
modifier restricts access to within the same assembly. - The
protected internal
modifier allows access within the same assembly or by derived classes. - The
new
keyword hides base class members in the derived class but does not override them.