C# Access Modifiers
Access modifiers control who can see and use a class or member. They are the main tool for encapsulation: keeping an object's internals private so that other code can only interact with it through a well-defined public surface.
The modifiers
| Modifier | Accessible from |
|---|---|
public |
Anywhere |
private |
Only inside the same class (the default for members) |
protected |
The same class and classes derived from it |
internal |
Anywhere in the same assembly (project) — the default for top-level classes |
protected internal |
Same assembly or derived classes |
private protected |
Derived classes in the same assembly |
In everyday code you mostly use public, private and occasionally protected or internal.
private versus public
A private member can only be used by code inside its own class. Trying to reach it from outside is a compile-time error:
class Safe
{
private string code = "1234";
}
var s = new Safe();
Console.WriteLine(s.code); // error CS0122: 'Safe.code' is inaccessible due to its protection levelIf you omit the modifier on a member, it is private. Writing it explicitly is still recommended for clarity.
Why encapsulate?
Compare two versions of a bank account. In the first, the balance is a public field and anyone can set it to anything:
using System;
var account = new OpenAccount();
account.Balance = 100;
account.Balance = -5000; // nothing stops this
Console.WriteLine(account.Balance);
class OpenAccount
{
public decimal Balance;
}-5000
In the second, the balance is private. The only way to change it is through methods that enforce the rules:
using System;
var account = new BankAccount("Ana");
account.Deposit(100);
Console.WriteLine(account.Withdraw(30));
Console.WriteLine(account.Withdraw(500));
Console.WriteLine(account.Deposit(-10));
Console.WriteLine(quot;{account.Owner}: {account.GetBalance()}");
class BankAccount
{
private decimal balance;
public string Owner { get; }
public BankAccount(string owner) => Owner = owner;
public bool Deposit(decimal amount)
{
if (amount <= 0) return false;
balance += amount;
return true;
}
public bool Withdraw(decimal amount)
{
if (amount <= 0 || amount > balance) return false;
balance -= amount;
return true;
}
public decimal GetBalance() => balance;
}True
False
False
Ana: 70
Now the account can never become negative, no matter what the calling code does. You can also change how the balance is stored later (say, as a list of transactions) without breaking callers, because they never touched the field directly.
protected
protected members are hidden from the outside world but visible to subclasses — useful for helpers a derived class needs:
using System;
var e = new Employee("Kai", 3000m);
Console.WriteLine(e.Describe());
class Person
{
protected string name;
public Person(string name) => this.name = name;
}
class Employee : Person
{
private readonly decimal salary;
public Employee(string name, decimal salary) : base(name) => this.salary = salary;
public string Describe() => quot;{name} earns {salary}"; // can use protected 'name'
}Kai earns 3000
From Main, e.name would not compile, but Employee can read it because it derives from Person.
Accessors with different access
Properties can have a public getter and a private setter, so everyone can read the value but only the class can change it:
using System;
var counter = new ClickCounter();
counter.Click();
counter.Click();
Console.WriteLine(counter.Clicks);
class ClickCounter
{
public int Clicks { get; private set; }
public void Click() => Clicks++;
}2
Tip: Start with the most restrictive access (
private) and open things up only when needed. It is easy to make a member more visible later, but hard to take visibility away once other code depends on it.
Test yourself
What is the default access level of a class member when no modifier is written?
- public
- internal
- private
- protected
Which modifier allows access from derived classes but not from unrelated classes?
- private
- protected
- internal
- public
public int Count { get; private set; } means…
- Nobody can read Count
- Anyone can read Count, only the class can change it
- Anyone can change Count
- Count is a constant