Tools
Log in
English

Learn to codeC#

C# access modifiers

Based on C# 12private protected · C# 7.2+

public, private, protected, internal and the two combined levels, in one table.

In one line

Words placed before classes and members to decide where they may be used. C# mixes two yardsticks, the class (same class, children) and the assembly (same project), into six levels.

Why you need it

If everything is public, other developers change hp or level from anywhere. When you later want to change a rule, so much code depends on those values that you cannot touch them.

Open only what is needed and you can change the closed parts freely. The compiler guards the boundaries, so nobody touches them by accident.

Everyday picture

Think of office building badges. The lobby is open to anyone (public), the office floors to company staff (internal), the lab to its team and teams spun off from it (protected), and the safe to one person (private). Each badge opens different doors.

Example code

Program.cs · 64 lines
var hero = new Knight("Aria");hero.TakeHit(30);hero.Rest();var rival = new Knight("Bram");rival.TakeHit(50);Console.WriteLine(hero.Status());Console.WriteLine($"Aria stronger: {hero.StrongerThan(rival)}");Console.WriteLine($"Max HP: {GameRules.MaxHp}"); static class GameRules{    public const int MaxHp = 100;    internal static int RestHeal => 10;} public class Character{    private int hp = GameRules.MaxHp;     public string Name { get; }    protected int Armor { get; set; } = 5;    private protected int Level { get; set; } = 1;    protected internal int Gold { get; set; }     public Character(string name)    {        Name = name;    }     public void TakeHit(int damage)    {        hp = Math.Max(0, hp - Math.Max(0, damage - Armor));    }     protected void Heal(int amount)    {        hp = Math.Min(GameRules.MaxHp, hp + amount);    }     public bool StrongerThan(Character other)    {        return hp > other.hp;    }     public string Status()    {        return $"{Name}: hp {hp}, armor {Armor}, level {Level}, gold {Gold}";    }} public class Knight : Character{    public Knight(string name) : base(name)    {        Armor = 12;    }     public void Rest()    {        Heal(GameRules.RestHeal);        Level++;        Gold += 5;    }}

Where each one is visible (assembly = the dll one project builds)

ModifierSame classChild, same assemblyOther class, same assemblyChild, other assemblyOther class, other assembly
publicOOOOO
protected internalOOOOX
internalOOOXX
protectedOOXOX
private protectedOOXXX
privateOXXXX

With no modifier, class members are private and top-level types are internal. C# has no "same package" level like Java; internal (same assembly) fills that role.

When to use it

  • Start with private and widen only when outside code truly needs it
  • Helper methods meant only for subclasses: protected
  • In a library, mark helper types used only inside the project internal so users never see them

protected internal opens when either condition holds (same project or a child); private protected needs both (a child in the same project).

private works per class, so code in a class can read another object’s private members (StrongerThan).

Common mistakes

Assuming no modifier means open

A class member with no modifier is private in C#. If outside code must read it, mark it public, preferably as a read-only property rather than a field.

Wrong

var box = new Box();Console.WriteLine(box.size); class Box{    int size = 3;}

What the C# compiler sayserror CS0122: 'Box.size' is inaccessible due to its protection level

Fixed

var box = new Box();Console.WriteLine(box.Size); class Box{    public int Size { get; } = 3;}

When you run it3

A public method exposing an internal type

Secret has no modifier, so it is internal. Returning it from a public method would hand other projects a value whose type they cannot see. Narrow the method or make the type public.

Wrong

public class Vault{    public Secret Open() => new Secret();} class Secret{}

What the C# compiler sayserror CS0050: Inconsistent accessibility: return type 'Secret' is less accessible than method 'Vault.Open()'

Fixed

public class Vault{    internal Secret Open() => new Secret();} class Secret{}

Using another object’s protected member through the parent type

protected opens only through your own child type. Through a Character variable you cannot change someone else’s Armor. Take a Paladin, or add a public method on the parent.

Wrong

class Paladin : Character{    public Paladin(string name) : base(name)    {    }     public void Shield(Character ally)    {        ally.Armor += 5;    }}

What the C# compiler sayserror CS1540: Cannot access protected member 'Character.Armor' via a qualifier of type 'Character'; the qualifier must be of type 'Paladin' (or derived from it)

Fixed

class Paladin : Character{    public Paladin(string name) : base(name)    {    }     public void Shield(Paladin ally)    {        ally.Armor += 5;    }}

Check yourself

Question 1

A knight with 12 armor takes a hit of 10. What is printed?

var k = new Knight("Cy");k.TakeHit(10);Console.WriteLine(k.Status());

Show the answer

Answer: 3. Cy: hp 100, armor 12, level 1, gold 0 10 - 12 is negative, and Math.Max(0, ...) turns it into 0. hp stays at 100.

Question 2

A field int n; inside a class has no modifier. What is its access level?

Show the answer

Answer: 3. private Members inside a class default to private, so c.n from outside fails with CS0122. Do not mix it up with the default for top-level classes, which is internal.

Questions people ask

What does no modifier mean?

Members inside a class are private; classes, records and interfaces at the top of a file are internal. There is no Java-style package level.

How far does internal reach?

Anywhere inside the same assembly, the dll built from one project. Other projects cannot see it, which suits helper types inside a library.

How do protected internal and private protected differ?

protected internal is visible to the same project or to children; private protected only to children inside the same project. The first needs either condition, the second both.