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:
Step 1: Identify computed property syntax
Computed properties use expression-bodied syntax with => and no setter.
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.