Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Properties: controlled access

โญ beginnerโณ 14 min read๐Ÿ“ Lesson 36 of 85

A public field hands out raw state; a property looks like a field but runs your code โ€” where invariants live.

The problem with public fields

A public field hands out raw state โ€” anyone can write account.Balance = -999; and your object is now lying. Fields should be private; the question is how outsiders read state they need.

Properties look like fields, run like methods

A property is accessed with field syntax but compiles to method calls:

class Account
{
    private decimal balance;                 // hidden state

    public decimal Balance                   // controlled access
    {
        get { return balance; }
        private set { balance = value; }     // only the class may change it
    }
}

Readers write account.Balance โ€” indistinguishable from a field at the call site. Writers hit your code, where the invariant lives. value is the incoming assignment inside set.

Auto-properties and validation

When both accessors just wrap a hidden field, let C# generate it:

public string Name { get; }             // get-only: set once in constructor
public int Hits { get; private set; }   // read anywhere, mutate inside methods

When a settable property must keep a rule, validate in set:

private int health;
public int Health
{
    get { return health; }
    set { health = Math.Clamp(value, 0, 100); }   // always in [0, 100]
}

Math.Clamp(value, min, max) pins a number into range โ€” every modern .NET has it. The habit that matters: state is private, behavior is public. Methods and setters are the only doors, and each door can enforce the rules.

โšก Now practice

Ready to Code
Class workoutCounters, shapes, temperatures, and players โ€” encapsulation under discriminating tests.
4 challenges ยท ยท ~40 min