Skip to content

Field keyword (preview) C# 13.0code reductioncorrectness

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

In C# 13.0, a new keyword field is introduced to reference 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.

Code

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

Notes

  • This feature is currently in preview and only available if you set <LangVersion>preview</LangVersion> in your project file.

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