C# Property
last modified November 6, 2026
In this article we show how to work with properties in C#.
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.
Properties use accessors through which the values of the private fields can be read, written or manipulated. Property reads and writes are translated to get and set method calls. Properties shield the data from the outside world while having a convenient field access syntax.
A get property accessor is used to return the property value, and
a set property accessor is used to assign a new value. The
init property accessor is used to assign a new value only during
object construction. The value keyword refers to the value that
is assigned by the set or init accessor.
Properties can be read-write (they have both a get and a set accessor), read-only (they have only a get accessor), or write-only (they have only a set accessor).
C# Property with backing field
The following example uses a property with a backing field.
User u = new();
u.Name = "Jane";
Console.WriteLine(u.Name);
class User
{
private string? _name;
public string? Name
{
get { return _name; }
set { _name = value; }
}
}
We have the Name property with the _name backing
field.
User u = new(); u.Name = "Jane"; Console.WriteLine(u.Name);
We create an instance of the User class. We access the property
using the field notation.
public string? Name
{
get { return _name; }
set { _name = value; }
}
We have a property that is called Name. It looks like a field
declaration with a block of specific accessors called get and
set. The get accessor returns the property value and
the set accessor assigns a new value. The value
keyword holds the value being assigned.
$ dotnet run Jane
C# Property validation
The main advantage of properties over public fields is that the accessors can contain logic. A common use is validating the assigned value.
var acc = new Account();
acc.Balance = 120m;
Console.WriteLine(acc.Balance);
try
{
acc.Balance = -50m;
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine(acc.Balance);
class Account
{
private decimal _balance;
public decimal Balance
{
get => _balance;
set
{
ArgumentOutOfRangeException.ThrowIfNegative(value);
_balance = value;
}
}
}
The Balance property refuses negative values.
set
{
ArgumentOutOfRangeException.ThrowIfNegative(value);
_balance = value;
}
The ThrowIfNegative helper, available since .NET 8, throws an
exception if the value is less than zero. The backing field is updated only
when the value is valid.
$ dotnet run
120
value ('-50') must be a non-negative value. (Parameter 'value')
Actual value was -50.
120
C# field keyword
C# 14 introduced the field keyword. It refers to a backing field
that the compiler creates for the property. We can add logic to one accessor
and keep the other one automatic, without declaring the field ourselves.
var u = new User { Name = " John Doe " };
Console.WriteLine($"[{u.Name}]");
u.Name = "Roger Roe\t";
Console.WriteLine($"[{u.Name}]");
class User
{
public string Name
{
get;
set => field = value.Trim();
} = "";
}
The Name property trims the assigned strings.
public string Name
{
get;
set => field = value.Trim();
} = "";
The get accessor is auto-implemented; it returns the backing
field. The set accessor stores a trimmed value into the field.
Such a property can also have an initializer.
$ dotnet run [John Doe] [Roger Roe]
C# read-only property
To create a read-only property, we omit the set accessor and provide only the get accessor.
var u = new User("John Doe", "gardener");
Console.WriteLine(u);
Console.WriteLine(u.Name);
class User(string name, string occupation)
{
public string Name
{
get { return name; }
}
public string Occupation => occupation;
public override string ToString()
{
return $"{Name} is a {Occupation}";
}
}
We have read-only properties. Once initialized in the constructor, they cannot be modified.
public string Name
{
get { return name; }
}
We make the property read-only by providing a get accessor only. The value comes from the parameter of the primary constructor.
public string Occupation => occupation;
This is a shorter form of a read-only property, using an expression body.
$ dotnet run John Doe is a gardener John Doe
C# auto-implemented properties
C# has auto-implemented or automatic properties. With automatic properties, the compiler transparently provides the backing fields for us.
var u = new User();
u.Name = "John Doe";
u.Occupation = "gardener";
Console.WriteLine($"{u.Name} is a {u.Occupation}");
class User
{
public string? Name { get; set; }
public string? Occupation { get; set; }
}
This code is much shorter. We have a User class with two
properties: Name and Occupation.
public string? Name { get; set; }
public string? Occupation { get; set; }
Here we have two automatic properties. There is no implementation of the accessors and there are no member fields. The compiler does the rest for us.
$ dotnet run John Doe is a gardener
C# private setter
Accessors can have their own access modifiers. A property with a public getter and a private setter can be read from anywhere but modified only inside the class.
var c = new Counter();
c.Increment();
c.Increment();
c.Increment();
Console.WriteLine(c.Count);
class Counter
{
public int Count { get; private set; }
public void Increment() => Count++;
}
The Count property can only be changed through the
Increment method.
public int Count { get; private set; }
Assigning to c.Count from outside the class would be a compile
error.
$ dotnet run 3
C# computed properties
A property does not need a backing field at all. A computed property calculates its value from other members each time it is read.
var r = new Rectangle { Width = 4, Height = 5 };
Console.WriteLine($"Area: {r.Area}");
Console.WriteLine($"Is square: {r.IsSquare}");
r.Height = 4;
Console.WriteLine($"Area: {r.Area}");
Console.WriteLine($"Is square: {r.IsSquare}");
class Rectangle
{
public double Width { get; set; }
public double Height { get; set; }
public double Area => Width * Height;
public bool IsSquare => Width == Height;
}
The Area and IsSquare properties are derived from
the width and height of the rectangle.
public double Area => Width * Height; public bool IsSquare => Width == Height;
The values are always up to date; after we change the height, the area and the square check reflect the change.
$ dotnet run Area: 20 Is square: False Area: 16 Is square: True
C# init-only property
The init keyword is used to create init-only properties; these
properties can be set only during object construction.
var u = new User("John Doe", "gardener");
Console.WriteLine(u);
var u2 = new User("Roger Roe", "driver") { Occupation = "pilot" };
Console.WriteLine(u2);
class User(string name, string occupation)
{
public string Name { get; init; } = name;
public string Occupation { get; init; } = occupation;
public override string ToString()
{
return $"{Name} is a {Occupation}";
}
}
We define two init-only properties. They are set when the object is created; after that, they cannot be changed.
public string Name { get; init; } = name;
public string Occupation { get; init; } = occupation;
The properties are initialized from the parameters of the primary constructor.
var u2 = new User("Roger Roe", "driver") { Occupation = "pilot" };
An init accessor can also be called from an object initializer, which runs after the constructor.
$ dotnet run John Doe is a gardener Roger Roe is a pilot
C# required properties
The required modifier forces the caller to set the property in an
object initializer.
var u = new User { Name = "John Doe", Occupation = "gardener" };
Console.WriteLine(u);
var u2 = new User { Name = "Roger Roe" };
Console.WriteLine(u2);
class User
{
public required string Name { get; init; }
public string? Occupation { get; init; }
public override string ToString()
{
return $"User {{ {Name}, {Occupation ?? "n/a"} }}";
}
}
The Name property is required; the Occupation
property is optional.
public required string Name { get; init; }
Omitting Name in the object initializer is a compile error.
Combined with init, this gives us an immutable property that is
guaranteed to be set. Assigning a value to an init-only property or forgetting a
required property produces the following errors:
error CS8852: Init-only property or indexer 'User.Name' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor. error CS9035: Required member 'User.Name' must be set in the object initializer or attribute constructor.
The program itself creates two valid users.
$ dotnet run
User { John Doe, gardener }
User { Roger Roe, n/a }
C# expression body definitions
Accessors can be simplified with expression body definitions. Expression body
definitions consist of the => symbol followed by the expression
to assign to or retrieve from the property.
var u = new User("John Doe", "gardener");
Console.WriteLine($"{u.Name} is a {u.Occupation}");
class User
{
private string _name;
private string _occupation;
public User(string name, string occupation)
{
_name = name;
_occupation = occupation;
}
public string Name
{
get => _name;
set => _name = value;
}
public string Occupation
{
get => _occupation;
set => _occupation = value;
}
}
We use expression body definitions for the accessors of the User
class properties.
public string Name
{
get => _name;
set => _name = value;
}
Each accessor consists of a single expression.
$ dotnet run John Doe is a gardener
C# static property
A static property belongs to the type rather than to an instance.
new Product("pen");
new Product("pencil");
new Product("eraser");
Console.WriteLine($"Products created: {Product.Count}");
class Product
{
public static int Count { get; private set; }
public string Name { get; }
public Product(string name)
{
Name = name;
Count++;
}
}
The Count static property keeps track of how many products were
created.
public static int Count { get; private set; }
The property is accessed through the class name: Product.Count.
The Name property has only a getter; such an auto-property can
be assigned only in the constructor.
$ dotnet run Products created: 3
C# virtual and interface properties
Properties can be marked with access modifiers like public,
private or protected. They can also be
static, abstract, virtual,
override, and sealed, just like methods. Interfaces
can declare properties too.
Shape[] shapes = [new Shape(), new Circle(), new Square()];
foreach (var shape in shapes)
{
Console.WriteLine(shape.Name);
}
IDescribable d = new Square();
Console.WriteLine(d.Description);
interface IDescribable
{
string Description { get; }
}
class Shape
{
public virtual string Name => "shape";
}
class Circle : Shape
{
public override string Name => "circle";
}
class Square : Shape, IDescribable
{
public override string Name => "square";
public string Description => $"A {Name} has four equal sides";
}
We define a virtual property and override it in derived classes. The
Square class also implements a property declared by an interface.
public virtual string Name => "shape";
The Name property is marked with the virtual
keyword.
public override string Name => "circle";
The derived class overrides the property. When we iterate over the array of
Shape objects, the overridden version is called for each derived
object.
interface IDescribable
{
string Description { get; }
}
The interface requires implementers to provide a readable
Description property.
$ dotnet run shape circle square A square has four equal sides
Source
Properties - programming guide
In this article we have covered C# properties.
Author
List all C# tutorials.