Today's frustration is with Visual Studio 2022 tooling and the release of C# 13/.NET 9.0. I know that these were released yesterday, but that is what made it frustrating.
Short Version:
Visual Studio 2022 (17.12.0) makes it look like the "field" keyword is usable. But it is only usable if you set the language version to "preview".
Note: this is specifically related to Visual Studio 2022 version 17.12.0 and .NET 9.0.100. I will keep an eye on future versions.
Quick Fix:
Set <LangVersion>preview</LangVersion>
in the project settings.
The field Keyword and Semi-Auto Properties
First, let's look at the "field" keyword. It is a C# proposal that was a potential feature of C# 13. You can see the proposal here: https://github.com/dotnet/csharplang/issues/140.
The "field" keyword would allow us to create semi-automatic properties. If you do some searches online, you will find lots of articles about this proposed feature.
Automatic Properties
Today we have automatic properties that give us a shorthand for creating properties:
public Customer OrderCustomer { get; set; }
When this is code is compiled, the compiler automatically creates a backing field with default methods for getting and setting the value. This saves us typing (and code) if we want default functionality.
If we want more functionality, then we would need to create a full property with backing field and custom getter and setter.
But the "field" keyword lets us do something a bit in between.
Semi-Automatic Property
Rather the creating a backing field manually, we can use the "field" keyword to represent the backing field in a getter or setter -- similar to how we use the "value" keyword to represent the incoming value in a setter.
Here's a sample of that:
public Customer OrderCustomer
{
get;
set => field = value ?? field;
}
Here I have modified the setter of the property. In this example, I check to see if the incoming "value" is null. If it is not null, then I assign the value to the backing field. But if "value" is null, then I keep the backing field unchanged.
The "get" is still automatic, so it has the default behavior of returning whatever is in the backing field.
Visual Studio 2022 Tooling Issue
So, here's the tooling issue that I ran into. I had heard about the "field" keyword and wanted to do some experimentation with it. I generally do not install preview versions of .NET, so I did this on release day (Nov 12, 2024).
Updating Visual Studio 2022 got me version 17.12.0 (along with .NET version 9.0.100).
When I started typing, Visual Studio helpfully gave me code completion, along with a note that "field" was a "keyword".
This is what I expected. But when I completed the code, I got an error: "The name 'field' does not exist in the current context"
This is when I went to do some searching and found that the GitHub proposal (linked above) is still "Open", meaning it is not yet complete.
The Fix
After some digging, I found that you can use the "field" keyword in Visual Studio 2022 (with .NET 9.0) as long as you have the language version set to "Preview".
The language version can be set as part of the project options:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
When this value is set, the error goes away, and the code works as expected.
Why I Care
I figure that if I ran into this issue that other folks may have as well. Messaging is really important to me, and if things are confusing, I tend to get frustrated.
From the Microsoft side, there is nothing in "What's new in C# 13" that would indicate that the "field" keyword is included. This is technically correct, but I wish that there was a little more as part of the "What's New" messaging (such as "What we mentioned but isn't ready yet" or "What's Still in Preview").
The reason I would like this additional messaging from Microsoft is that when the "field" keyword was proposed, there were a lot of articles written about it. And when it was available in the preview versions of .NET and C#, more articles were written about it.
I appreciate that the feature was held back from release -- this normally indicates that more work needs to be done, and I am glad that the language teams do whatever they can to make sure that they get the feature right.
But here I have Visual Studio 2022 telling me that "field" is a keyword in the code completion. But it is only valid when using the correct language preview setting. It would be great if Visual Studio could have the note "field Keyword (preview)" instead of "field Keyword". That would at least give me a clue that it is not yet valid.
Instead, Visual Studio is offering to auto-complete to code that will not compile.
Wrap Up
I expect that this will be fixed rather quickly. It may be that the feature is released out-of-band (meaning before C# 14).
In the meantime, you can set the language version to "preview" if you want to experiment with the "field" keyword and semi-automatic properties.
Happy Coding!
No comments:
Post a Comment