NDepend Blog

Improve your .NET code quality with NDepend

C# 13 Semi-Auto Properties

December 4, 2024 2 minutes read

C# 13 Semi Auto Properties

C# 13 introduced an exciting feature to boost developer productivity and code clarity: semi-auto properties. This new feature solves the trade-off between the ease of auto-implemented properties with the versatility of manual ones.

Update (December 4, 2024): This feature was originally planned for inclusion in C# 13. However, despite the release of C# 13 and .NET 9 in November 2024, it is still not functioning as expected. The code examples in this article still require <LangVersion>preview</LangVersion> to be set in your .csproj file.

Reminder: Auto-Property and Backing Field

Here is a usual C# auto-property:

If we compile this code we can see that a backing field is created for us by the C# compiler:

Problem solved by the C# 13 Semi Auto Properties

Including logic in a property’s getter or setter is common for handling validation, default value, computation, notification, or lazy loading. However, this requires defining the backing field manually, as auto-property syntax can’t be used:

The C# 13 Semi Auto Properties syntax proposes to skip the backing field explicit declaration and use the new C# keyword field to refer to it:

Notice that in the sample code above we cannot use the auto-accessor get; instead of get => field; or get { return field; }.

Benefits of C# 13 Semi Auto Properties

  • Reduced Boilerplate Code: Eliminates the need for private backing fields. This results in more concise code.
  • Improved Readability: We don’t need to care for the name of the backing field, the field keyword standard makes the code clearer.
  • Property-Scoped Field: A private property field is only visible within the property, preventing unintended use elsewhere in the parent class. This encapsulation benefit might be the main one of C# 13 Semi Auto Properties because this solves quite an error-prone situation.

Potential field Keyword Breaking Change

If we try out C# 13 Semi Auto Properties on sharplab.io (Branch C# Next: Semi-auto properties)  we can observe that if your code uses a field or variable named field, it takes precedence over the new field keyword:
C# 13 semi auto property field keyword collision
And here is what happens if we comment int field;:
C# 13 semi auto property field keyword no collision

Conclusion

C# 13 Semi-Auto Properties and the keyword field is another welcomed tiny and subtle language improvement. We will likely soon see a Roslyn Analyzer that identifies opportunities to simplify code using the field keyword, along with an automatic code fixer to apply this syntax across your application.

 

This article is brought to you by the team behind NDepend — a proven .NET static analysis tool for improving code maintainability, security, and overall quality. Whether you’re modernizing a legacy .NET application or starting fresh in C#, get started with your free full-featured trial today!

Comments:

  1. An Average Democrat says:

    Semi auto?? That sounds scary, and as a Democrat I lack basic cognitive reasoning or even the faintest modicum of self reliance, so I can’t be bothered to read further or educate myself, so we should just ban it! Think of the children! No one needs a high capacity assault semi auto property!

  2. This does mean that you should never, and I mean never ever, use the variable named field in your source any more (your private field should be named _field anyway) as that could potentially alter the behavior of your code.

Comments are closed.