Bird
Raised Fist0

Which of these is a valid computed property syntax in C#?

easy🧠 Conceptual Q2 of Q15
C Sharp (C#) - Properties and Encapsulation
Which of these is a valid computed property syntax in C#?
Apublic int Total { get; set; } = Price * Quantity;
Bpublic int Total() { return Price * Quantity; }
Cpublic int Total => Price * Quantity;
Dpublic int Total = Price * Quantity;
Step-by-Step Solution
Solution:
  1. Step 1: Identify computed property syntax

    Computed properties use expression-bodied syntax with => and no setter.
  2. Step 2: Evaluate options

    public int Total => Price * Quantity; uses correct syntax. public int Total { get; set; } = Price * Quantity; tries to assign in auto-property, which is invalid for computed. public int Total() { return Price * Quantity; } is a method, not a property. public int Total = Price * Quantity; is a field, not a property.
  3. Final Answer:

    public int Total => Price * Quantity; -> Option C
  4. Quick Check:

    Computed property syntax = expression-bodied property [OK]
Quick Trick: Use => for computed properties without set [OK]
Common Mistakes:
MISTAKES
  • Using get; set; with expression
  • Writing methods instead of properties
  • Assigning values directly to properties

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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