C# Properties
A property is a member that looks like a field to the code using it, but runs code — accessors — when it is read or written. Properties are how C# classes expose data while staying in control of it. They are one of the most-used features of the language.
From field to property
A property usually wraps a private backing field. The get accessor returns a value; the set accessor receives the new value in an implicit parameter called value.
using System;
var p = new Person();
p.Name = " grace hopper ";
Console.WriteLine(quot;[{p.Name}]");
p.Age = 85;
p.Age = -3; // rejected by the setter
Console.WriteLine(p.Age);
class Person
{
private string _name = "";
private int _age;
public string Name
{
get { return _name; }
set { _name = value.Trim(); }
}
public int Age
{
get => _age;
set
{
if (value >= 0) _age = value;
}
}
}[grace hopper]
85
Callers simply write p.Age = 85 — they do not see that validation happens behind the scenes.
Auto-implemented properties
When no extra logic is needed, let the compiler create the backing field for you:
using System;
var movie = new Movie { Title = "Arrival", Year = 2016 };
movie.Rating = 7.9;
Console.WriteLine(quot;{movie.Title} ({movie.Year}): {movie.Rating}, id {movie.Id}");
class Movie
{
public string Title { get; set; } = "Untitled"; // with initial value
public int Year { get; set; }
public double Rating { get; set; }
public Guid Id { get; } = Guid.Empty; // read-only auto-property
}Arrival (2016): 7.9, id 00000000-0000-0000-0000-000000000000
Why not just use public fields? Because a property can later gain validation, logging or change notifications without changing the code that uses it. Properties are also what data binding, serialisers and many libraries look for.
Read-only, init-only and computed properties
| Declaration | Who can set it | When |
|---|---|---|
{ get; set; } |
Anyone | Any time |
{ get; private set; } |
Only the class | Any time |
{ get; } |
Only constructors / initializer | During construction |
{ get; init; } |
Anyone, but only in an object initializer | During construction |
=> expression |
Nobody — computed on every read | — |
using System;
var order = new Order { Id = 1042, UnitPrice = 12.5m, Quantity = 4 };
Console.WriteLine(quot;Order {order.Id}: {order.Total}");
order.Quantity = 6;
Console.WriteLine(quot;Order {order.Id}: {order.Total}");
// order.Id = 7; // error CS8852: init-only property
class Order
{
public int Id { get; init; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public decimal Total => UnitPrice * Quantity; // computed property
}Order 1042: 50.0
Order 1042: 75.0
init lets callers use object-initializer syntax while still making the object immutable afterwards. Marking a property required (C# 11) additionally forces callers to set it.
Validation with exceptions
For truly invalid values, throwing an exception from the setter is better than silently ignoring them:
using System;
var t = new Thermostat();
t.Target = 21;
Console.WriteLine(t.Target);
try
{
t.Target = 45;
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
class Thermostat
{
private int _target = 20;
public int Target
{
get => _target;
set
{
if (value < 5 || value > 30)
throw new ArgumentOutOfRangeException(nameof(value), quot;Target must be 5-30, got {value}.");
_target = value;
}
}
}21
Target must be 5-30, got 45. (Parameter 'value')
The field keyword
Newer C# versions offer a contextual field keyword so you can add logic to just one accessor without declaring the backing field yourself — for example public string Name { get; set => field = value.Trim(); }. It is available from C# 14; on older compilers, declare the backing field manually as shown above.
Tip: Naming: properties use PascalCase (
FirstName), backing fields use_camelCase(_firstName).
Test yourself
Inside a set accessor, what holds the value being assigned?
- input
- newValue
- value
- this
Which property can be set in an object initializer but not changed afterwards?
- { get; set; }
- { get; init; }
- { get; private set; }
- => 42
What is public int Area => Width * Height;?
- A field
- A method
- A read-only computed property
- A constant