Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Computed properties in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Computed Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of computed property in C#
What is the output of this C# program that uses a computed property?
C Sharp (C#)
public class Rectangle {
    public int Width { get; set; }
    public int Height { get; set; }
    public int Area => Width * Height;
}

class Program {
    static void Main() {
        var rect = new Rectangle { Width = 4, Height = 5 };
        System.Console.WriteLine(rect.Area);
    }
}
A20
B9
C0
DCompilation error
Attempts:
2 left
💡 Hint
The Area property calculates Width multiplied by Height.
Predict Output
intermediate
2:00remaining
Computed property with backing field
What will be the output of this C# code using a computed property with a backing field?
C Sharp (C#)
public class Circle {
    private double radius;
    public double Radius {
        get => radius;
        set => radius = value;
    }
    public double Diameter => 2 * Radius;
}

class Program {
    static void Main() {
        var c = new Circle { Radius = 3 };
        System.Console.WriteLine(c.Diameter);
    }
}
A3
B9
CRuntime error
D6
Attempts:
2 left
💡 Hint
Diameter is twice the radius.
Predict Output
advanced
2:00remaining
Computed property with conditional logic
What is the output of this C# program with a computed property that uses conditional logic?
C Sharp (C#)
public class Temperature {
    public double Celsius { get; set; }
    public string State => Celsius >= 100 ? "Gas" : Celsius <= 0 ? "Solid" : "Liquid";
}

class Program {
    static void Main() {
        var t = new Temperature { Celsius = 50 };
        System.Console.WriteLine(t.State);
    }
}
A"Solid"
B"Gas"
C"Liquid"
DCompilation error
Attempts:
2 left
💡 Hint
Check the temperature ranges for state.
Predict Output
advanced
2:00remaining
Computed property with side effect
What will be printed by this C# program where a computed property has a side effect?
C Sharp (C#)
public class Counter {
    private int count = 0;
    public int Count {
        get {
            count++;
            return count;
        }
    }
}

class Program {
    static void Main() {
        var c = new Counter();
        System.Console.WriteLine(c.Count);
        System.Console.WriteLine(c.Count);
    }
}
A1\n2
BCompilation error
C1\n1
D0\n1
Attempts:
2 left
💡 Hint
Each access to Count increments count before returning.
🧠 Conceptual
expert
2:00remaining
Understanding computed properties behavior
Which statement about computed properties in C# is TRUE?
AComputed properties always cache their result after first calculation.
BComputed properties execute their code every time they are accessed.
CComputed properties cannot use conditional logic.
DComputed properties can store values internally without a backing field.
Attempts:
2 left
💡 Hint
Think about when the code inside a computed property runs.

Practice

(1/5)
1. What is a computed property in C#?
public int Area => Width * Height;
easy
A. A property that calculates its value from other data automatically
B. A property that stores a fixed value in memory
C. A method that returns a value
D. A variable that can be changed anytime

Solution

  1. Step 1: Understand the property syntax

    The property uses the lambda syntax with =>, which means it calculates the value when accessed.
  2. Step 2: Recognize computed property behavior

    It does not store a value but returns Width * Height each time, so it is computed automatically.
  3. Final Answer:

    A property that calculates its value from other data automatically -> Option A
  4. Quick Check:

    Computed property = calculates value [OK]
Hint: Look for => syntax to spot computed properties [OK]
Common Mistakes:
  • Thinking computed properties store values
  • Confusing methods with properties
  • Assuming computed properties can be set directly
2. Which of the following is the correct syntax for a read-only computed property in C#?
easy
A. public int Total() { return Price + Quantity; }
B. public int Total { get { return Price * Quantity; } }
C. public int Total => Price + Quantity { get; set; }
D. public int Total { set { Price = value; } }

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    public int Total { get { return Price * Quantity; } } -> Option B
  4. Quick Check:

    Read-only computed property = get only [OK]
Hint: Read-only properties have only get accessor [OK]
Common Mistakes:
  • Using set accessor in read-only properties
  • Confusing methods with properties
  • Incorrect expression body syntax
3. What is the output of this code?
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);
medium
A. 12
B. 7
C. 0
D. Error

Solution

  1. Step 1: Identify property values

    Width is 3 and Height is 4 as set by default.
  2. Step 2: Calculate computed property Area

    Area returns Width * Height = 3 * 4 = 12.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Multiply Width and Height for Area [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Assuming default values are zero
  • Confusing property with method call
4. Find the error in this computed property code:
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; } }
}
medium
A. Circumference should not use Math.PI
B. Radius property is missing get accessor
C. Computed property Diameter cannot be assigned in set accessor
D. No error, code is correct

Solution

  1. Step 1: Check computed property Diameter

    Diameter is read-only computed property with only get accessor (=>). It cannot be assigned a value.
  2. Step 2: Analyze Circumference set accessor

    Circumference tries to set Diameter, which is not allowed because Diameter has no set accessor.
  3. Final Answer:

    Computed property Diameter cannot be assigned in set accessor -> Option C
  4. Quick Check:

    Read-only property cannot be set [OK]
Hint: Computed properties with => are read-only [OK]
Common Mistakes:
  • Trying to set read-only computed properties
  • Ignoring property accessors
  • Confusing methods with properties
5. You want to create a computed property IsAdult in a Person class that returns true if Age is 18 or more, otherwise false. Which code correctly implements this?
hard
A. public bool IsAdult() { return Age >= 18; }
B. public bool IsAdult { get { return Age > 18; } }
C. public bool IsAdult { get; set; } = Age >= 18;
D. public bool IsAdult => Age >= 18;

Solution

  1. Step 1: Understand requirement for computed property

    IsAdult should return true if Age is 18 or more, false otherwise, without storing a value.
  2. 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.
  3. Final Answer:

    public bool IsAdult => Age >= 18; -> Option D
  4. Quick Check:

    Age >= 18 for IsAdult [OK]
Hint: Use => with condition for simple computed properties [OK]
Common Mistakes:
  • Using > instead of >= for age check
  • Trying to set computed property value
  • Confusing methods with properties