Access Modifiers in C# with Program Examples
Learn about access modifiers in C# such as public, private, protected and internal, with practical examples.
What are Access Modifiers?
Access modifiers are keywords that control the visibility and accessibility of classes, methods, and other members from other parts of a program.
public and private
The public modifier makes a member accessible from anywhere, while private restricts access to only within the same class, which is the most restrictive option.
protected and internal
The protected modifier allows access within the class and its derived classes, while internal restricts access to code within the same assembly.
Combining Modifiers
C# also allows combined modifiers like protected internal and private protected, giving finer control over how a member can be accessed across classes and assemblies.
One Program Showing Every Modifier
The example below places members with different modifiers in a base class, then tries to reach them from a derived class and from unrelated code. Comments show what compiles.
public class Account
{
public string Owner = "Asha"; // anywhere
private decimal balance = 1000; // only inside Account
protected string Branch = "Pune"; // Account and derived classes
internal int RegionCode = 7; // same assembly
protected internal string Ifsc = "ABC01"; // same assembly OR derived classes
private protected bool IsFrozen; // derived classes in the same assembly
public decimal GetBalance() => balance; // controlled access to private data
}
public class SavingsAccount : Account
{
public void Show()
{
Console.WriteLine(Owner); // OK
Console.WriteLine(Branch); // OK: protected
Console.WriteLine(IsFrozen); // OK: derived, same assembly
// Console.WriteLine(balance); // error: private
}
}
public class Report
{
public void Print(Account a)
{
Console.WriteLine(a.RegionCode); // OK: internal, same assembly
// Console.WriteLine(a.Branch); // error: not a derived class
}
}
Access at a Glance
| Modifier | Same class | Derived, same assembly | Other class, same assembly | Derived, other assembly | Other class, other assembly |
|---|---|---|---|---|---|
| public | Yes | Yes | Yes | Yes | Yes |
| protected internal | Yes | Yes | Yes | Yes | No |
| internal | Yes | Yes | Yes | No | No |
| protected | Yes | Yes | No | Yes | No |
| private protected | Yes | Yes | No | No | No |
| private | Yes | No | No | No | No |
Read each row as a question: "Can code in this place see the member?" The columns move from the closest code (the class itself) to the most distant (an unrelated class in another project). Only public reaches every column.
A simple way to remember the two combined forms: protected internal is the wider one (either condition is enough), while private protected is the narrower one (both conditions must hold).
Defaults and Less-Known Rules
- Class and struct members with no modifier are private.
- Top-level classes with no modifier are internal. They can only be public or internal (or
file, which limits a type to its source file in newer C# versions). - Interface members are public by default.
- A member cannot be more visible than its type: a public method cannot return an internal class.
- When overriding, keep the same access level as the base member.
[assembly: InternalsVisibleTo("MyApp.Tests")]lets a test project see internal members without making them public.
Choosing Well in Real Code
Start with the most restrictive level and open up only when another part of the program truly needs access. This keeps the public surface small, so changes inside a class are less likely to break other code.
- Prefer private fields exposed through properties, for example
public decimal Balance { get; private set; }. - Avoid public fields in shared libraries; they cannot later gain validation without breaking callers.
- Use
internalfor helpers that other projects should not depend on. - Use
protectedsparingly, because every derived class becomes tied to that member. - Review modifiers during code review: a member made public "just for now" tends to stay public forever.