Advertisement
Beginner C# Programming, Properties in C#

What are the different types of properties available in C#?

Short answer

C# supports several types of properties, including read write properties that have both get and set accessors, read only properties that only expose a get accessor, write only properties that only expose a set accessor, auto implemented properties that let the compiler generate…

Save C# Programming mock interview
Advertisement
AModel answer
C# supports several types of properties, including read write properties that have both get and set accessors, read only properties that only expose a get accessor, write only properties that only expose a set accessor, auto implemented properties that let the compiler generate the backing field automatically, and computed properties that calculate their return value dynamically instead of storing it directly in a field.
public string FullName { get; }           // read only
public string Password { set; private get; } // write focused
public string Email { get; set; }            // read write, auto implemented
public int Total => Price * Quantity;        // computed property
Real-world example
An order class exposes a computed Total property that always multiplies price by quantity on the fly, guaranteeing the total is never out of sync since it is calculated fresh every time it is accessed rather than stored separately.
Likely follow-ups
  • What is an auto implemented property and how does it differ from a regular property?
  • When would you use a computed property instead of storing a value directly?
Related topics