NDepend Blog

Improve your .NET code quality with NDepend

C# 13 Semi-Auto Properties

July 8, 2024 2 minutes read

C# 13 Semi Auto Properties

C# 13 will introduce 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.

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; }. I hope this is a limitation that will be fixed in the final C# 13 version:

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
However this compiler heuristic might change because in the official C# team proposal it is stated that:
Since field is a keyword and not an identifier, it can only be “shadowed” by an identifier using the normal keyword-escaping route: @field. All identifiers named field declared within property accessor bodies can safeguard against breaks when upgrading from C# versions prior to 13 by adding the initial @.

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.

 

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.