Properties
A property looks like a field from outside, but it is a pair of methods called accessors: get runs on every read and set runs on every write. Follow a Thermostat object whose set uses Math.Clamp to keep its private field between 5 and 30, so writing 99 stores 30; then meet a computed, read-only property (86) and an auto-implemented one.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01What runs when your code says t.Target = 24; and Target is a property?
Assigning to a property calls its set accessor. Inside set, the new value arrives in the keyword value, and set decides what to store.
02Target's set is _target = Math.Clamp(value, 5, 30);. After t.Target = 99; what is in _target?
Math.Clamp returns the maximum when value is above it, so 99 becomes 30. The bad number never reaches the field.
03public int Fahrenheit => _target * 9 / 5 + 32; What happens if you write t.Fahrenheit = 50;?
Fahrenheit has only a get accessor and no storage, so it is read-only: the compiler refuses t.Fahrenheit = 50; with CS0200 and the program never runs.
04What does public int Floor { get; set; } give you?
With { get; set; } and no bodies, the compiler creates a private hidden field and simple get and set accessors for it. Use it when no check is needed.
05Why is _target declared without public?
Class members are private by default. Keeping the field private means outside code must go through Target, so the clamp in set can never be skipped.
Glossary
- Field
- A variable that belongs to an object, such as int _target. Usually private.
- Property
- A member that looks like a field from outside but is backed by accessor methods, such as Target.
- Accessor
- One of the methods behind a property: get runs when the property is read, set runs when it is written.
- value
- The keyword inside a set accessor that holds the new value being assigned.
- Backing field
- The private field a property stores its data in. For an auto-property the compiler creates a hidden one.
- Read-only property
- A property with a get but no set. Code outside the class cannot assign to it (error CS0200); a get-only auto-property can still be set in the constructor.
- Auto-implemented property
- A property written { get; set; } with no bodies; the compiler writes the backing field and both accessors.
- Math.Clamp
- A .NET method that returns a value kept between a minimum and a maximum.
Notes and sources
- The program is real C#: put the five statements and then the class in Program.cs, and dotnet run prints 30 and 86. In one file, top-level statements must come before any class.
- Math.Clamp(value, min, max) returns min when value is below min, max when it is above max, and value otherwise. It exists since .NET Core 2.0.
- With two ints, / drops any fraction (it truncates toward zero). 30 °C comes out exact (86), but 21 °C would print 69, not 69.8.
- Members of a class with no access modifier are private. Writing private int _target; means the same thing. The leading underscore is only a naming habit.
- The compiler gives the hidden backing field a name you cannot type in C#, so only the property can reach it. C# 14 adds the field keyword, which lets a set accessor use that hidden field directly.
- Under the hood the compiler turns the accessors into two methods, get_Target and set_Target(int value), which is why reading or writing a property can run code.
- Microsoft Learn: Properties (C# programming guide)
- Microsoft Learn: Using properties (get, set and value)
- Microsoft Learn: The value keyword
- Microsoft Learn: Auto-implemented properties
- Microsoft Learn: Expression-bodied members
- Microsoft Learn: Access modifiers
- Microsoft Learn: Math.Clamp method
- Microsoft Learn: Arithmetic operators (integer division)
- Microsoft Learn: Property declaration errors (CS0200)
- Microsoft Learn: What's new in C# 14 (the field keyword)
Quiz Unlocks after the last step.