1.2.13 Public and Private Access Modifiers

Access Modifiers Control Who Can Reach a Member

C# can mark members with access modifiers such as:

C#
public

and:

C#
private

These modifiers help control how code outside the class can access its internal state.

public Allows Broader Access

A public field such as:

C#
public int jerseyNumber;

can be accessed by code that has a Player object and is allowed by the normal C# type rules:

C#
player1.jerseyNumber = 7;

This direct access is common in early course examples.

private Restricts Direct Outside Access

A private field:

C#
private int jerseyNumber;

is not directly accessible from unrelated outside code in the same way.

That helps the class protect its internal state.

Access Modifiers Support Encapsulation

Encapsulation asks:

Which class owns this information, and who should be allowed to change it directly?

private supports a tighter boundary.

public exposes the member more broadly.

The correct choice depends on the current design.

Do Not Make Everything Private Without a Plan

Later modules teach methods and properties that can provide controlled access to private state.

At this point, use the exact access required by the current course design.

Do not make fields private if the current learner task still requires direct access and no replacement mechanism has been taught.

Read Compiler Feedback as Evidence

If code attempts to access a private field from an invalid location, the compiler can report the access problem.

That message helps you distinguish:

Plain text
field does not exist

from:

Plain text
field exists but is not accessible here

Access is part of the class design, not merely style.