Computed properties in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's see how the time it takes to get a computed property changes as the data grows.
We want to know how the cost of calculating a property depends on the size of the input.
Analyze the time complexity of the following code snippet.
public class Numbers
{
public int[] Values { get; set; }
public int Sum => Values.Sum();
}
This code defines a computed property Sum that calculates the total of all numbers in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Summing all elements in the array inside the computed property.
- How many times: Once each time the
Sumproperty is accessed, it loops through all elements.
When the array has more numbers, the sum takes longer because it adds each number one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items; double the items, double the work.
Time Complexity: O(n)
This means the time to get the computed sum grows in a straight line with the number of elements.
[X] Wrong: "Accessing the computed property is instant no matter the array size."
[OK] Correct: Each time you ask for the sum, the program adds up all numbers again, so bigger arrays take more time.
Understanding how computed properties work helps you explain performance in real code and shows you think about efficiency clearly.
"What if we stored the sum once and updated it only when the array changes? How would the time complexity change when accessing the sum?"
Practice
public int Area => Width * Height;Solution
Step 1: Understand the property syntax
The property uses the lambda syntax with =>, which means it calculates the value when accessed.Step 2: Recognize computed property behavior
It does not store a value but returns Width * Height each time, so it is computed automatically.Final Answer:
A property that calculates its value from other data automatically -> Option AQuick Check:
Computed property = calculates value [OK]
- Thinking computed properties store values
- Confusing methods with properties
- Assuming computed properties can be set directly
Solution
Step 1: Identify read-only computed property syntax
public int Total { get { return Price * Quantity; } } uses a property with only a get accessor returning a calculation, which is correct.Step 2: Check other options for errors
public int Total { set { Price = value; } } only has set, so not read-only. public int Total => Price + Quantity { get; set; } mixes expression body with get/set incorrectly. public int Total() { return Price + Quantity; } is a method, not a property.Final Answer:
public int Total { get { return Price * Quantity; } } -> Option BQuick Check:
Read-only computed property = get only [OK]
- Using set accessor in read-only properties
- Confusing methods with properties
- Incorrect expression body syntax
class Box {
public int Width { get; set; } = 3;
public int Height { get; set; } = 4;
public int Area => Width * Height;
}
var box = new Box();
Console.WriteLine(box.Area);Solution
Step 1: Identify property values
Width is 3 and Height is 4 as set by default.Step 2: Calculate computed property Area
Area returns Width * Height = 3 * 4 = 12.Final Answer:
12 -> Option AQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Assuming default values are zero
- Confusing property with method call
public class Circle {
public double Radius { get; set; }
public double Diameter => Radius * 2;
public double Circumference { get { return 2 * Math.PI * Diameter; } set { Diameter = value; } }
}Solution
Step 1: Check computed property Diameter
Diameter is read-only computed property with only get accessor (=>). It cannot be assigned a value.Step 2: Analyze Circumference set accessor
Circumference tries to set Diameter, which is not allowed because Diameter has no set accessor.Final Answer:
Computed property Diameter cannot be assigned in set accessor -> Option CQuick Check:
Read-only property cannot be set [OK]
- Trying to set read-only computed properties
- Ignoring property accessors
- Confusing methods with properties
IsAdult in a Person class that returns true if Age is 18 or more, otherwise false. Which code correctly implements this?Solution
Step 1: Understand requirement for computed property
IsAdult should return true if Age is 18 or more, false otherwise, without storing a value.Step 2: Check each option
public bool IsAdult => Age >= 18; uses expression-bodied property correctly with >= 18. public bool IsAdult { get { return Age > 18; } } uses > 18 (wrong condition). public bool IsAdult { get; set; } = Age >= 18; tries to set property with Age comparison, which is invalid. public bool IsAdult() { return Age >= 18; } is a method, not a property.Final Answer:
public bool IsAdult => Age >= 18; -> Option DQuick Check:
Age >= 18 for IsAdult [OK]
- Using > instead of >= for age check
- Trying to set computed property value
- Confusing methods with properties
