C# Properties
An introduction to properties in C#, explaining how they wrap fields and control access to an object's data.
What is a Property?
A property is a member that provides a flexible way to read, write, or compute the value of a private field, exposed through get and set accessors.
Auto-Implemented Properties
Auto-implemented properties let you declare a property without manually writing a backing field, since the compiler creates one automatically behind the scenes.
Read-only and Write-only Properties
A property can be made read-only by omitting the set accessor, or write-only by omitting the get accessor, depending on how the data should be used.
Why Use Properties Instead of Fields?
Properties allow you to add validation or logic when data is read or written, without changing how the member is used from outside the class.
How a Full Property Works Behind the Scenes
A full property pairs a private field with two accessors. The get accessor runs whenever code reads the property, and the set accessor runs whenever code assigns to it. Inside set, the contextual keyword value holds whatever the caller tried to assign.
To write one by hand, follow these steps:
- Declare a private field, usually with a leading underscore, such as
_price. - Declare a public property of the same type with a PascalCase name, such as
Price. - Return the field from the
getaccessor. - Check
valueinsideset, then either store it in the field or throw an exception.
public class Product
{
private decimal _price;
public string Name { get; set; } = "";
public decimal Price
{
get { return _price; }
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value), "Price cannot be negative.");
_price = value;
}
}
}
var p = new Product { Name = "Keyboard", Price = 1499m };
p.Price = -10m; // throws ArgumentOutOfRangeException
From the caller's side, p.Price = 1499m looks exactly like setting a field. The validation stays hidden inside the class, so you can tighten the rules later without touching any calling code.
Private-Set, Init-Only and Computed Properties
Modern C# offers several shapes beyond a plain get and set. Choose the one that matches how the data is allowed to change over the life of the object.
| Shape | Syntax | Good for |
|---|---|---|
| Private setter | public int Count { get; private set; } | Values that only the class itself should change |
| Init-only | public string Id { get; init; } | Values fixed once the object has been created |
| Required | public required string Email { get; init; } | Values the caller must supply in an object initializer |
| Computed | public decimal Total => Price * Qty; | Values derived from other data and never stored |
A computed property has no backing field at all. It recalculates every time it is read, so keep the work inside it cheap. If the calculation is slow or talks to a database, a method such as CalculateTotal() signals the cost more honestly to other developers.
The init accessor needs C# 9 or later, and the required modifier needs C# 11 or later, so check the language version your project targets.
Property Mistakes That Trip Up Beginners
- A setter that calls itself. Writing
set { Price = value; }instead of_price = value;makes the property call itself again and again until the program crashes with a stack overflow. - Public fields "for now". Turning a public field into a property later changes the compiled contract, which can break other assemblies built against the old version. Start with a property instead.
- Heavy work or side effects in a getter. Developers expect reading a property to be quick and harmless. The debugger also evaluates getters when you hover over them, which can trigger surprising behaviour.
- Exposing a mutable list. A get-only
List<T>property still lets callers add and remove items. ReturnIReadOnlyList<T>when outside code should only read the collection.
Quick design checklist
- Should outside code change this value? If not, use
private setorinit. - Does the value need checking before it is stored? Use a full property with validation.
- Is it derived from other members? Use an expression-bodied computed property.
- Does it need nothing special at all? An auto-implemented property is enough.