Bird
0
0

How can you create a computed property that returns the discounted price applying a 10% discount to Price, but only if Price is above 100?

hard🚀 Application Q9 of 15
C Sharp (C#) - Properties and Encapsulation
How can you create a computed property that returns the discounted price applying a 10% discount to Price, but only if Price is above 100?
Apublic decimal DiscountedPrice => Price > 100 ? Price * 0.9m : Price;
Bpublic decimal DiscountedPrice { get; set; } = Price > 100 ? Price * 0.9m : Price;
Cpublic decimal DiscountedPrice() => Price > 100 ? Price * 0.9m : Price;
Dpublic decimal DiscountedPrice { set => Price > 100 ? Price * 0.9m : Price; }
Step-by-Step Solution
Solution:
  1. Step 1: Use conditional operator in computed property

    Computed property can use ternary operator to return discounted or original price.
  2. Step 2: Check options

    public decimal DiscountedPrice => Price > 100 ? Price * 0.9m : Price; correctly uses expression-bodied property with condition. public decimal DiscountedPrice { get; set; } = Price > 100 ? Price * 0.9m : Price; uses auto-property with initializer, which won't update dynamically. public decimal DiscountedPrice() => Price > 100 ? Price * 0.9m : Price; is a method, not a property. public decimal DiscountedPrice { set => Price > 100 ? Price * 0.9m : Price; } wrongly uses set accessor.
  3. Final Answer:

    public decimal DiscountedPrice => Price > 100 ? Price * 0.9m : Price; -> Option A
  4. Quick Check:

    Computed property with condition uses => and ?: [OK]
Quick Trick: Use ?: inside => for conditional computed properties [OK]
Common Mistakes:
MISTAKES
  • Using auto-properties with initializers
  • Confusing methods with properties
  • Using set accessor incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes