2.7.1 Understanding Properties: Purpose and Differences From Fields

Understanding Properties: Purpose and Differences From Fields

The coach window needs to read player information. C# properties provide field-like access syntax while running accessor code defined by the class.

Distinguish storage from access
Three different members
Member What it does
Field Stores state in this example
Property Defines accessors used when reading or assigning
Method Expresses an operation called with parentheses

A property may read a private backing field, validate a proposed value, or calculate an answer. It does not have to store a separate value itself. The following diagram pages show the notation before the technical implementation lessons.

Read the caller without assuming storage

Compare the two forms below conceptually. Do not paste them together: they show alternative interfaces.

Method-style query
int turns = player.GetNumberOfTurns();
Property-style query
int turns = player.NumberOfTurns;

The second line can run a get accessor even though it has no call parentheses. A property declaration determines what code runs and whether assignment is available. Looking only at the caller does not prove the member is a field.

Preserve meaningful actions

Keep RecordTurn as an action. A NumberOfTurns property can report the result without letting callers assign arbitrary totals. A getter-only count can change later because an action changes its underlying state.

Apply the idea
Check your understanding