Skip to content

Field keyword C# 13.0code reductioncorrectness

A new keyword to reference the synthesized backing field of an auto property.

A new keyword field references the synthesized backing field of an auto property. This is useful when you need to access the backing field directly, for example, when you want to implement custom logic in the getter or setter of a property.

WARNING

The field keyword is a preview feature in C# 13 and requires <LangVersion>preview</LangVersion>. It becomes generally available in C# 14.0.

Code

C#
class Post
{
    public string Title
    {
        get;
        set => field = !String.IsNullOrEmpty(value)
                            ? value
                            : throw new ArgumentException("Must not be empty.", nameof(value));
    }
}
C#
class Post
{
    private string title;
    public string Title
    {
        get => title;
        set => title = !String.IsNullOrEmpty(value)
                            ? value
                            : throw new ArgumentException("Must not be empty.", nameof(value));
    }
}

WARNING

If you have a field named field in your class, you must access the backing field of an auto property by using @field instead.

More information