C# 13, or .Net 9, is scheduled for release in November 2024, and some of the new features have already been finalized, so let's preview one of them today:
Author's note: This feature was released with C# 13, but it is still in preview, so use it with caution!
Semi-auto properties
As you know, C# added theAutomatic propertiesFor this feature, let's review the autoproperty : the
public string Name { get; set; }
The above code is equivalent to the following fully manually implemented property:
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
Then in C# 6.0, lambda expressions were supported to simplify things a bit
private string _name;
public string Name
{
get => _name;
set => _name = value;
}
As you can see, the automatic attribute feature greatly facilitates the writing of attributes, omitting a lot of tedious code, manual attributes need to manually declare the corresponding private fields behind the attribute.
However, when we need to customize some logic when getting or setting a property, the automatic properties can't do anything about it and have to degenerate into manually implemented properties, for example
private string _name;
public string Name
{
get => _name;
set => _name = "Hello " + value;
}
C# 13 now solves this problem by introducing the field keyword, which allows customization of the logic without having to declare the corresponding field yourself.
The above code can be written as:
public string Name
{
get => field;
set => field = "Hello " + value;
}
Further, we can change get to be written as a fully automated attribute, which is also supported by the
public string Name
{
get;
set => field = "Hello " + value;
}
As you can see, semi-automatic attributes will greatly facilitate the writing of code on fields that have custom logic. It improves the readability of the code as well as avoids the contamination of field names.
As a side note, field is a context keyword that is only valid in attribute blocks, e.g. using field in a method will have no effect
public string Test()
{
return field; // ERROR: The name 'field' does not exist in the current context
}
Disruptive Updates
Note that introducing this field keyword is a destructive update to, for example, your original code:
public int Age
{
get
{
var field = 18;
return field;
}
}
Variables with the name field that existed in the original code will change behavior when upgrading to C# 13, and the property will now return the field default value."0" Instead of the expected "18 "
You need to make the following changes to prevent the variable field from being recognized as a keyword
public int Age
{
get
{
var field = 18;
return @field;
}
}
wind up
This feature has gone through several drafts, and the field keyword has been changed several times before it was finalized, and it was finally released this year.
It may seem like a simple feature, but with a thousand relationships behind it and the existence of destructive updates, it's clear that the C# team has a lot to think about and take care of when rolling out features, so it's inevitably going to be slowed down.
Finally, this feature is currently live in Visual Studio 2022 17.12 Preview 3.0, which requires the C# language version to be set to preview, so you can try it out for yourself.
consultation
csharplang/proposals/ at main · dotnet/csharplang
Proposal: field keyword in properties · Issue #140 · dotnet/csharplang