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.
C# code does not run on this page. Run your edited code on your own computer with dotnet run.
Where each one is visible (assembly = the dll one project builds)
Modifier
Same class
Child, same assembly
Other class, same assembly
Child, other assembly
Other class, other assembly
public
O
O
O
O
O
protected internal
O
O
O
O
X
internal
O
O
O
X
X
protected
O
O
X
O
X
private protected
O
O
X
X
X
private
O
X
X
X
X
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 = newBox();Console.WriteLine(box.size);classBox{int size = 3;}
What the C# compiler sayserror CS0122: 'Box.size' is inaccessible due to its protection level
Fixed
var box = newBox();Console.WriteLine(box.Size);classBox{publicintSize { 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.
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.
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)
A knight with 12 armor takes a hit of 10. What is printed?
var k = newKnight("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.