Blog
C# 13 semi-auto properties
Next November with the release of .NET 9 a new version of C# will be released. Since the number 13 is my personal lucky number (I was born and married on the 13th š) I am happy that it is the 13th version of C#. In this version a new feature called semi-auto properties is added. In this blog I will explain what it is and how you can take advantage of it.
1ļøā£What are auto-implemented propertiesĀ
In C# version 3 (already 10 versions ago!) the feature auto-implemented properties was added. With this feature we got the possibility to add properties to our classes and didnāt have to write methods around it ourselves. Letās take a look at an example. In C# 2 you had to write the following code:
private string name;
public string GetName()
{
return name;
}
public void SetName(string name)
{
this.name = name;
}
When auto-implemented properties was added in C# 3 this code could be reduced to the following:
public string Name { get; set; }
In the end this code will be lowered during compile time to the same code as the example above (a private backing field and a get and a set method will be generated). This feature meant a lot of code reduction that fits for a lot of cases. There is however one disadvantage that hasn’t been addressed since then. It is the fact that when using auto-implemented properties you can’t have any validation or control when the get or set of a property is called. When you want to do this you still have to add a private backing field and add a code block to the get or set part of the property. For example:
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value.ToUpper();
}
}
As you see in the example the overhead is that you have to add a private backing field and add an implementation block to the get and the set part of the property.
2ļøā£Semi-auto properties to the rescue Ā
Now with C# 13, the team that is responsible for C# is introducing a solution that will make it easy to use properties and also add some logic inside the get or the set part of the property. To achieve this a new keyword field is added to the language. This keyword can only be used in getters and setters of properties. It will represent the private backing field that is generated when using properties. You will now be able to write the following code:
public string Name
{
get => field.ToUpper();
set => field = value;
}
As you can see you donāt have to add a private backing field and the field keyword can be used to access the automatically generated backing field.
3ļøā£Ā Isnāt this a breaking change that can break my existing code? Ā
Yes, it is! When you have code in C# 12 or earlier and you have some members that have the name field that code will get a different meaning in C# 13 and can be breaking (field is now a reserved keyword). This can also be the reason why Microsoft has waited a long time before adding this. Making breaking changes is always challenging. But there is also a solution to it. When your code breaks because you used the field keyword in existing code you can fix this by simply adding an @ symbol in front of it. In that way the compiler knows it is the name of a variable in your code. It would look like this:
private string @field;
4ļøā£Ā Can I already try this?Ā
Yes, you can but at the moment it does not yet work in the latest preview of Visual Studio (version 17.11.0 preview 2.0). You can already try it by using the website https://sharplab.io/. On this website you can select to use the branch C# Next: Semi-auto-properties (27 Oct 2022). A direct link to an example there:
Sharplab.io
ā©Conclusion Ā
With C# 13 the language again gets better and better. Semi-auto properties reduce writing plumbing code and make it easier to read and write C#. The fact that this feature is a breaking change and can potentially break your code is in my opinion not a big issue. In case you run into this there is an easy fix by adding the @ sign.
I hope you liked this blog post. In case you have any questions or suggestions feel free to contact me.
Sources:
– Video about this subject by Nick Chapsas: Youtube Nick Chapsas
– MS Build 2024 presentation: MS Build 2024 presentation
Written by: Hans Enthoven
Hans is a software developer with over 20 years of experience. As a software developer he has lots of experience developing for the Microsoft platform. In these 20 years Hans has carried out many different projects in different environments. Hans describes himself as a professional software engineer with a lot of experience within Microsoft technology. Next to developing Hans also coaches people on the job and likes to give presentations to share his knowledge with other people.
Mission: Building structured, reusable and maintainable software that that matters in this society.
Want to know more about our experts? Contact us!
