Recall & Review
beginner
What is a computed property in C#?
A computed property is a property that calculates its value dynamically using code in its getter, instead of storing a value directly.
Click to reveal answer
beginner
How do you define a computed property in C#?
You define a computed property by providing only a getter with code that returns a calculated value, without a backing field.
Click to reveal answer
beginner
Example: What does this computed property do?
public int DoubleAge => Age * 2;
This property returns twice the value of the Age property whenever accessed. It does not store a value but computes it on the fly.
Click to reveal answer
intermediate
Can computed properties have setters in C#?
No, computed properties usually only have getters because their value is calculated. If you add a setter, it must store a value somewhere.
Click to reveal answer
intermediate
Why use computed properties instead of fields?
Computed properties keep data consistent by calculating values on demand, avoiding errors from storing outdated values.
Click to reveal answer
What keyword is used to define a computed property with a single expression in C#?
✗ Incorrect
The '=>' symbol defines an expression-bodied member, often used for computed properties.
Which of these is true about computed properties in C#?
✗ Incorrect
Computed properties calculate and return values dynamically when accessed.
What happens if you try to assign a value to a computed property with only a getter?
✗ Incorrect
Computed properties with only getters cannot be assigned to; this causes a compile-time error.
Which is a benefit of using computed properties?
✗ Incorrect
Computed properties save memory by calculating values on demand instead of storing them.
How do you write a computed property that returns the length of a string property 'Name'?
✗ Incorrect
Option A uses an expression-bodied computed property to return the length of 'Name'.
Explain what a computed property is and why it is useful in C#.
Think about properties that calculate values instead of storing them.
You got /4 concepts.
Write a simple computed property in C# that returns the full name by combining first and last names.
Use string interpolation or concatenation inside the getter.
You got /4 concepts.