0
0
C Sharp (C#)programming~10 mins

Computed properties in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Computed properties
Define class with computed property
Create object instance
Access computed property
Run get accessor code
Return computed value
Use value in program
The flow shows how a computed property is defined, accessed, and returns a value calculated on the fly.
Execution Sample
C Sharp (C#)
class Rectangle {
  public int Width { get; set; }
  public int Height { get; set; }
  public int Area => Width * Height;
}

var rect = new Rectangle { Width = 3, Height = 4 };
Console.WriteLine(rect.Area);
This code defines a Rectangle with Width and Height, and a computed property Area that multiplies them. It prints the area.
Execution Table
StepActionVariable/PropertyValueExplanation
1Create Rectangle objectrectRectangle instanceObject with Width and Height properties created
2Set Widthrect.Width3Width property set to 3
3Set Heightrect.Height4Height property set to 4
4Access computed property Arearect.Area12Area getter runs: 3 * 4 = 12
5Print AreaOutput12Area value 12 printed to console
💡 All steps complete, program ends after printing computed Area
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
rect.Width0 (default)3333
rect.Height0 (default)0444
rect.AreaN/AN/AN/A1212
Key Moments - 2 Insights
Why does rect.Area not store a value but still returns 12?
Because Area is a computed property with only a get accessor that calculates Width * Height each time it is accessed, it does not store a value but computes it on demand (see execution_table step 4).
What happens if Width or Height changes after accessing Area once?
Area will reflect the new product because it computes the value every time it is accessed, not just once (not shown here but implied by the computed property behavior).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of rect.Area at step 4?
A0
B7
C12
DUndefined
💡 Hint
Check the 'Value' column for step 4 in execution_table where Area is accessed.
At which step is rect.Height set to 4?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Variable/Property' columns in execution_table.
If rect.Width was changed to 5 after step 3, what would rect.Area be at step 4?
A12
B20
C7
D9
💡 Hint
Area computes Width * Height each time; changing Width to 5 and Height is 4 means 5*4=20.
Concept Snapshot
Computed properties in C# use get accessors to calculate values on demand.
Syntax: public int PropertyName => expression;
They do not store values but compute each time accessed.
Useful for values derived from other properties.
Example: Area => Width * Height.
Full Transcript
This example shows a Rectangle class with Width and Height properties and a computed property Area that multiplies them. When we create a Rectangle object and set Width to 3 and Height to 4, accessing Area runs the get accessor which calculates 3 times 4 and returns 12. This value is printed. The computed property does not store the area but calculates it fresh each time it is accessed. This means if Width or Height changes later, Area will reflect the new product automatically.