Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The field keyword

Reference for the C# 14 field keyword: add logic to a property accessor and use the compiler-generated backing field without declaring one yourself.

Inside a property accessor, field refers to the property's hidden backing field. It arrived in C# 14 (.NET 10). Before it, the moment you needed a bit of logic in a get or set, such as trimming a string or raising an event, you had to give up the one-line auto-property, declare a private field and write both accessors by hand.

With field, you keep the auto-property style and only write the accessor that needs logic. The compiler still generates the storage; you just get a name for it inside the accessors.

C#
public string Name
{
    get;
    set => field = value.Trim();
}

Try it

C# Validation and normalization in a setter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Lazy values and change notification

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public T Property
{
    get => field;                 // or just: get;
    set => field = Transform(value);
}
// optional initializer for the backing field:
public T Other { get; set => field = value; } = initialValue;
Part Description
field A contextual keyword usable only in get, set and init accessors of a property or indexer that has no explicit backing field of its own.
Accessors At least one accessor must use field, or both can be bodies. A bodiless accessor (get;) is auto-implemented over the same field.
Initializer = value after the closing brace initializes the backing field, as with an auto-property.

Errors: non-nullable string properties that never get assigned produce warning CS9264 ("Non-nullable property must contain a non-null value when exiting constructor"); give the property an initializer or make it required. Using field outside a property accessor is an ordinary identifier.

Usage notes

  • The backing field has the same type as the property and is private to the class. It has no name you can use outside the accessors, and reflection sees a compiler-generated name.
  • field is a contextual keyword. If your class already has a member called field, the identifier inside accessors now means the backing field. Write @field or this.field to reach the member. The compiler warns about this breaking change (CS9258).
  • It also works for init accessors, for read-only-style properties with a get that uses ??= for lazy initialization, and for indexers.
  • Use it for small jobs: trimming, range checks, change notification, lazy defaults. When the field needs a different type than the property (for example a List<T> field exposed as IReadOnlyList<T>), declare an explicit field.
  • Auto-property semantics survive: you can still assign the property in a constructor or object initializer, and records with field properties keep working.
  • In C# 13 the feature is a preview. On a compiler older than C# 14 the same code treats field as a plain name and fails with CS0103.