Fields vs Properties
A property looks like a field from outside but runs code inside, so a class can let others read and set a value while it checks every change.
Apprentice · C# · OOP · Updated
After this lesson you can
- Expose a private field through a property with get and set
Key takeaways
- A property is read and assigned like a field, but its
getruns when it's read and itssetwhen it's assigned. - In a
set,valueis whatever was assigned: infridge.Temperature = 34;,valueis 34. - A
setcan check the value first, so the object never holds one that makes no sense, such as a refrigerator at 95 degrees. - The usual C# style is private camelCase fields, such as
temperature, and public PascalCase properties, such asTemperature.
Private fields keep an object's data safe, but other code often needs to read it, and sometimes to change it. In the last lessons, GetTemperature() did the reading. Many languages write a pair of methods for each value: GetTemperature() and SetTemperature(...). C# has a better tool: the property.
A property is a field with gatekeepers
From outside, a property looks just like a public field: you read it and assign to it with no parentheses. Inside, it runs code: a get when it's read, and a set when it's assigned.
Refrigerator fridge = new Refrigerator();
fridge.Temperature = 34;
Console.WriteLine( quot;Set to {fridge.Temperature} degrees");
public class Refrigerator
{
private int temperature = 37;
public int Temperature
{
get { return temperature; }
set { temperature = value; }
}
}
Set to 34 degrees
- The field
temperature(camelCase, private) holds the value. It's called the property's backing field. - The property
Temperature(PascalCase, public) is the way in. - In
set, the wordvalueis whatever was assigned: infridge.Temperature = 34;,valueis 34.
Why not a public field?
A public field lets anyone put anything in it. A property's set can check the value first, so the object can never be put into a state that makes no sense:
Refrigerator fridge = new Refrigerator();
fridge.Temperature = 34;
fridge.Temperature = 95;
fridge.Temperature = -10;
Console.WriteLine( quot;Temperature: {fridge.Temperature}");
public class Refrigerator
{
private int temperature = 37;
public int Temperature
{
get { return temperature; }
set
{
if (value >= 32 && value <= 45)
{
temperature = value;
}
}
}
}
Temperature: 34
95 and -10 aren't safe refrigerator temperatures, so the set ignores them, and the temperature stays at 34. Code that uses the class doesn't have to remember the rule; the class enforces it.
That's encapsulation at work: the data is private, and every change passes through code the class controls. If the rule changes later, it changes in one place, and every caller stays the same.
Fields and properties, side by side
| Field | Property | |
|---|---|---|
| Holds a value | yes | through a backing field (or works one out) |
| Can run code when read or set | no | yes |
| Usual access | private |
public |
| Naming | camelCase: temperature |
PascalCase: Temperature |
The usual C# style: fields are private, and anything others need is a property.
Do it: keep the oven at a temperature it can reach
The kitchen's oven can be set from 170 to 500 degrees. Anything else is a typo, such as 4250 for 425, and the oven should ignore it. Put that rule in a property, so no code can set the oven to a temperature it can't reach.
The class Oven has a private field, temperature, that starts at 350. Its property Temperature is the way in:
value: inside theset, the temperature the code assigned, anint(inoven.Temperature = 425;,valueis425).
For example:
Oven oven = new Oven();
oven.Temperature = 425;
oven.Temperature = 4250;
oven.Temperature = 100;
Console.WriteLine(oven.Temperature);
| Line | Temperature after it |
Why |
|---|---|---|
new Oven() |
350 | the field's starting value |
= 425 |
425 | from 170 to 500, so it's kept |
= 4250 |
425 | above 500, so it's ignored |
= 100 |
425 | below 170, so it's ignored |
So the last line prints 425.
Your job: finish the property Temperature in the class Oven.
- Its
getreturns the fieldtemperature. - Its
setstoresvalueintemperatureonly whenvalueis from 170 to 500. Both 170 and 500 are allowed. - Any other value is ignored: the temperature stays what it was.
Practiced live on the lesson page: write the C# right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
Properties let a class share its data the easy way, with plain reads and assignments, while keeping the final say over every change.