C# field keyword
last modified November 6, 2026
In this article we show how to use the field keyword in C#.
The field contextual keyword, introduced in C# 14, refers to the
compiler-generated backing field of a property. It lets us add logic to one
or both accessors of an auto-implemented property without declaring the
backing field ourselves. Such properties are called field-backed
properties.
Key characteristics of field:
- It can be used only in the
get,set, andinitaccessors of a property. - The compiler creates a private backing field with an unspeakable name, which no other member of the class can access.
- One accessor can stay automatic (
get;orset;) while the other has a body. - A field-backed property can have an initializer, which assigns the backing field directly.
- It is not available in indexers, which have no backing storage; using it there results in error CS0103.
A property before and after
Before C# 14, adding logic to a single accessor meant converting the whole property to a full property with an explicit backing field.
var p1 = new PersonOld { Name = " Alice " };
var p2 = new Person { Name = " Alice " };
Console.WriteLine($"[{p1.Name}]");
Console.WriteLine($"[{p2.Name}]");
class PersonOld
{
private string _name = "";
public string Name
{
get => _name;
set => _name = value.Trim();
}
}
class Person
{
public string Name
{
get;
set => field = value.Trim();
} = "";
}
Both classes trim the assigned name; the second one does it with the
field keyword.
private string _name = "";
public string Name
{
get => _name;
set => _name = value.Trim();
}
The classic approach needs a private field, and the getter must be written out even though it only returns the field.
public string Name
{
get;
set => field = value.Trim();
} = "";
With field, the getter stays automatic and the setter assigns the
trimmed value to the backing field. The initializer sets the field to an empty
string so that the non-nullable property is never null.
$ dotnet run [Alice] [Alice]
Validation in the setter
The most common use of field is validating or normalizing
assigned values.
var product = new Product { Name = "Coffee", Price = 12.5m };
Console.WriteLine($"{product.Name}: {product.Price}");
try
{
product.Price = -3;
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine(e.Message);
}
product.Name = "";
Console.WriteLine($"[{product.Name}]");
class Product
{
public string Name
{
get;
set => field = string.IsNullOrWhiteSpace(value) ? "Unknown" : value;
} = "Unknown";
public decimal Price
{
get;
set
{
ArgumentOutOfRangeException.ThrowIfNegative(value);
field = value;
}
}
}
The Name property replaces blank names with "Unknown", and the
Price property rejects negative prices.
set
{
ArgumentOutOfRangeException.ThrowIfNegative(value);
field = value;
}
An accessor can have a block body. The
ArgumentOutOfRangeException.ThrowIfNegative helper throws an
exception with a descriptive message for invalid values.
$ dotnet run
Coffee: 12.5
value ('-3') must be a non-negative value. (Parameter 'value')
Actual value was -3.
[Unknown]
Init-only properties
The field keyword works in init accessors too. The
property can then be set only in an object initializer, and the value is
validated at that moment.
var config = new Configuration { ConnectionString = "Server=localhost" };
Console.WriteLine(config.ConnectionString);
try
{
var invalid = new Configuration { ConnectionString = " " };
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
class Configuration
{
public required string ConnectionString
{
get;
init
{
ArgumentException.ThrowIfNullOrWhiteSpace(value);
field = value;
}
}
}
The ConnectionString property must be set when the object is
created and must not be blank.
public required string ConnectionString
The required modifier makes the compiler check that every object
initializer sets the property.
ArgumentException.ThrowIfNullOrWhiteSpace(value);
The helper throws an ArgumentException for null,
empty, or whitespace-only strings.
$ dotnet run Server=localhost The value cannot be an empty string or composed entirely of whitespace. (Parameter 'value')
Lazy initialization
A getter can compute the value on first access and store it in the backing field.
var cache = new DataCache();
Console.WriteLine(cache.Data);
Console.WriteLine(cache.Data);
cache.Data = "custom data";
Console.WriteLine(cache.Data);
class DataCache
{
public string Data
{
get => field ??= Load();
set;
}
private static string Load()
{
Console.WriteLine("loading...");
return "loaded data";
}
}
The Data property loads its value only when it is first read.
get => field ??= Load(); set;
The null-coalescing assignment operator ??= calls
Load only when the backing field is null. The setter
stays automatic. The output shows that the data is loaded once. This simple
form is not thread-safe; for concurrent access use Lazy<T>.
$ dotnet run loading... loaded data loaded data custom data
INotifyPropertyChanged
Classes that notify a user interface about changes, such as view models in
WPF, MAUI, or Avalonia, used to need a backing field for each property. The
field keyword makes them much shorter.
using System.ComponentModel;
using System.Runtime.CompilerServices;
var user = new User();
user.PropertyChanged += (_, e) => Console.WriteLine($"changed: {e.PropertyName}");
user.Name = "Alice";
user.Age = 30;
user.Name = "Alice";
user.Age = 31;
Console.WriteLine($"{user.Name}, {user.Age}");
class User : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public string Name
{
get;
set => SetField(ref field, value);
} = "";
public int Age
{
get;
set => SetField(ref field, value);
}
private void SetField<T>(ref T storage, T value, [CallerMemberName] string? name = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value)) return;
storage = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
The User class raises the PropertyChanged event when
a property gets a new value.
set => SetField(ref field, value);
The backing field can be passed by reference. The helper compares the old and new values, stores the new value, and raises the event.
private void SetField<T>(ref T storage, T value, [CallerMemberName] string? name = null)
The CallerMemberName attribute fills in the name of the property
that called the method. Assigning the same name again raises no event, as the
output shows.
$ dotnet run changed: Name changed: Age changed: Age Alice, 31
Initializers bypass the setter
A property initializer writes the backing field directly. The setter logic does not run for it.
var s1 = new Settings();
Console.WriteLine(s1.Volume);
s1.Volume = 150;
Console.WriteLine(s1.Volume);
var s2 = new Settings { Volume = 150 };
Console.WriteLine(s2.Volume);
class Settings
{
public int Volume
{
get;
set => field = Math.Clamp(value, 0, 100);
} = 120;
}
The Volume setter clamps values to the range 0 to 100, but the
initializer sets 120.
set => field = Math.Clamp(value, 0, 100); } = 120;
The new object has a volume of 120, outside the allowed range. Assignments, including those in object initializers, go through the setter and are clamped. Make sure that initial values are valid.
$ dotnet run 120 100 100
Name conflicts
Code written before C# 14 may contain a member named field. Inside
a property accessor, the name now refers to the backing field, and the
compiler warns about the change.
var c = new Counter();
c.Count = 5;
Console.WriteLine(c.Count);
Console.WriteLine(c.Describe());
class Counter
{
private string field = "counter";
public int Count
{
get;
set => field = value < 0 ? 0 : value;
}
public string Describe() => $"{this.field}: {Count}";
}
The Counter class has a private member named field.
set => field = value < 0 ? 0 : value;
In the setter, field is the synthesized backing field of
Count, not the string member. The compiler emits warning CS9258.
public string Describe() => $"{this.field}: {Count}";
Outside property accessors, field is an ordinary identifier. To
reach the member inside an accessor, write this.field or
@field. Renaming the member is usually the best fix.
$ dotnet run conflict.cs(14,16): warning CS9258: In language version 14.0, the 'field' keyword binds to a synthesized backing field for the property. To avoid generating a synthesized backing field, and to refer to the existing member, use 'this.field' or '@field' instead. 5 counter: 5
Best practices
- Use
fieldwhen one accessor needs logic; keep plain auto-implemented properties otherwise. - Keep accessor logic short: validation, normalization, notification, or lazy loading.
- Remember that initializers bypass the setter.
- Keep an explicit backing field when other members of the class need to access it.
Source
field keyword - language reference
In this article we have used the field keyword of C# 14 to work
with the backing fields of properties.
Author
List all C# tutorials.