C# 14: The field Keyword – Or Should I Say: I Had a Dream?

In the last post about C# 14, you learned about Extension Members. In this post you will learn about the new field keyword of C# 14. I was already dreaming about it in the year 2008, and I blogged about my dream of half-automated properties. With C# 14, this dream has now come true. So, let’s look at this new feature.

Auto Property or Full Property

Before C# 14, there was nothing in between an auto property (=automatically implemented property) and a full property. Let’s say you have this auto property:

public class Person
{
    public string? FirstName{ get; set; }
}

As you might know, behind the scenes a private field gets generated by the C# compiler that stores the value for this FirstName property. You can see that private field in the Intermediate Language Disassembler (ILDASM) tool. You can open this tool by opening a Developer Command Prompt from Visual Studio via the menu Tools • CommandLine • Developer Command Prompt. Type ildasm into the command prompt and press ENTER to open this tool. When you drag and drop a DLL onto the tool that has a Person class like the one from above in it, then you can see the backing field of the FirstName property.

The screenshot below shows a DLL with the Person class from above in the ILDASM tool. I highlighted the generated backing field for the FirstName property .

Ok, so far so good. This means for auto properties that the backing field is automatically generated for you behind the scenes. But now, what if you want to include something in the setter of such an auto property? For example, what if you want to raise a PropertyChanged event in the setter, which is a common scenario in ViewModel classes? If you want to do that, you have to switch to a full property with an explicit backing field like this:

public class Person
{
    private string? _firstName;

    public string? FirstName
    {
        get { return _firstName; }
        set
        { 
            _firstName = value; 
            // Add your additional logic here
        }
    }
}

Back in 2008, my idea was to have something in between these two states of an auto property and a full property. I thought that this would be possible by providing a keyword that allows you to access the generated backing field of an auto property. And exactly such a keyword has been introduced with C# 14.

The field Keyword

The field keyword introduced with C# 14 gives you access to the generated backing field of an auto property. This means that the full property from above can also be written like below. Note how the field keyword is used to access the generated backing field of the auto property.

public class Person
{
    public string? FirstName
    {
        get { return field; }
        set { field = value; }
    }
}

And of course, if you don’t need to access the backing field in the getter or setter, you can use an automatically implemented getter or setter. Below you see the same property as above, but now with an auto getter.

public class Person
{
    public string? FirstName
    {
        get;
        set { field = value; }
    }
}

This means that a typical property in a ViewModel that raises a PropertyChanged event in the setter can be written much shorter and without an explicit backing field. Before C# 14, such a property looked like below:

private string? _firstName;

public string? FirstName
{
    get { return _firstName; }
    set
    {
        _firstName = value;
        OnPropertyChanged(nameof(FirstName));
    }
}

Since C# 14, you can write it more compact like below. Note that there is no explicit backing field and no explicit implementation of the getter.

public string? FirstName
{
    get;
    set
    {
        field = value;
        OnPropertyChanged(nameof(FirstName));
    }
}

Conclusion

C# 14’s field keyword is a small, but powerful addition to the C# language. It means that you can implement custom logic in a setter without creating a full-blown .NET property with an explicit backing field. I like it and I’ll definitely use it for those projects that I switch to .NET 10 or later.

I mean, of course I will use this feature, as it looks exactly like what I already wanted back in 2008.

Happy coding,
Thomas

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.