Welcome to The Coding College! In this tutorial, we’ll dive into access modifiers in C#, which control the visibility and accessibility of classes, methods, and other members within a program. Mastering access modifiers is key to implementing encapsulation, a fundamental principle of object-oriented programming (OOP).
What Are Access Modifiers in C#?
Access modifiers are keywords used to specify the accessibility of a class or its members (fields, methods, properties, etc.). They define where these members can be accessed within the program.
Types of Access Modifiers in C#
C# provides the following access modifiers:
- Public
- Private
- Protected
- Internal
- Protected Internal
- Private Protected
1. Public
The public
modifier allows access from anywhere in the program or other programs that reference it.
Example:
public class Car
{
public string Brand;
public void DisplayBrand()
{
Console.WriteLine($"Brand: {Brand}");
}
}
public class Program
{
public static void Main()
{
Car car = new Car();
car.Brand = "Tesla"; // Accessible
car.DisplayBrand(); // Accessible
}
}
2. Private
The private
modifier restricts access to within the same class. This is the most restrictive access level and is the default for class members if no modifier is specified.
Example:
public class Car
{
private string Brand;
public void SetBrand(string brand)
{
Brand = brand; // Accessible within the class
}
public void DisplayBrand()
{
Console.WriteLine($"Brand: {Brand}");
}
}
public class Program
{
public static void Main()
{
Car car = new Car();
car.SetBrand("Tesla"); // Accessible
car.DisplayBrand(); // Accessible
// car.Brand = "Tesla"; // Error: Cannot access private member
}
}
3. Protected
The protected
modifier allows access within the same class and any derived class.
Example:
public class Vehicle
{
protected string Type;
public void SetType(string type)
{
Type = type;
}
}
public class Car : Vehicle
{
public void DisplayType()
{
Console.WriteLine($"Vehicle Type: {Type}"); // Accessible in derived class
}
}
public class Program
{
public static void Main()
{
Car car = new Car();
car.SetType("Electric");
car.DisplayType();
// car.Type = "Electric"; // Error: Protected member not accessible here
}
}
4. Internal
The internal
modifier allows access within the same assembly but not from another assembly. It’s useful for restricting access to code libraries or modules.
Example:
internal class Car
{
public string Brand;
public void DisplayBrand()
{
Console.WriteLine($"Brand: {Brand}");
}
}
public class Program
{
public static void Main()
{
Car car = new Car();
car.Brand = "Tesla"; // Accessible within the same assembly
car.DisplayBrand(); // Accessible
}
}
5. Protected Internal
The protected internal
modifier combines protected
and internal
. It allows access within the same assembly and also from derived classes in other assemblies.
Example:
public class Vehicle
{
protected internal string Type;
public void DisplayType()
{
Console.WriteLine($"Vehicle Type: {Type}");
}
}
public class Car : Vehicle
{
public void SetType(string type)
{
Type = type; // Accessible in derived class
}
}
6. Private Protected
The private protected
modifier allows access within the same class and its derived classes, but only if they are in the same assembly.
Example:
public class Vehicle
{
private protected string Type;
public void SetType(string type)
{
Type = type;
}
}
public class Car : Vehicle
{
public void DisplayType()
{
Console.WriteLine($"Vehicle Type: {Type}"); // Accessible in derived class within the same assembly
}
}
Access Modifiers for Classes
Top-Level Classes:
- Only
public
andinternal
are allowed.
Nested Classes:
- Can use any of the access modifiers.
Comparison of Access Modifiers
Modifier | Same Class | Derived Class | Same Assembly | Other Assembly |
---|---|---|---|---|
Public | ✔ | ✔ | ✔ | ✔ |
Private | ✔ | ✘ | ✘ | ✘ |
Protected | ✔ | ✔ | ✘ | ✘ |
Internal | ✔ | ✘ | ✔ | ✘ |
Protected Internal | ✔ | ✔ | ✔ | ✔ (Derived) |
Private Protected | ✔ | ✔ | ✘ | ✘ |
Best Practices
- Use the most restrictive modifier possible to maintain encapsulation and security.
- Use
private
for internal fields or methods unless they need to be accessed externally. - Use
protected
only when the class is designed for inheritance. - Avoid making fields
public
. Instead, use properties to control access.
Real-World Example
Let’s apply these modifiers to a banking application:
public class BankAccount
{
private decimal balance; // Only accessible within the class
public string AccountHolder { get; set; } // Accessible anywhere
protected string AccountType; // Accessible in derived classes
public void Deposit(decimal amount)
{
balance += amount; // Modify balance through a public method
}
public decimal GetBalance()
{
return balance; // Securely access private data
}
}
Conclusion
Access modifiers in C# are a fundamental tool for implementing encapsulation and controlling the visibility of code. By understanding and applying them effectively, you can write secure, maintainable, and modular programs.
For more in-depth tutorials, visit The Coding College. Happy coding! 🎉