Member-only story
🦈 C# 12 Preview: Primary Constructors, Optional Lambda P’s and Using Anything
Update: C# 12 is not yet released. I’ve adjusted the article to reflect the current status
In October 2022 C# 11 was released. Today we can check out some of the C# 12 features as previews.
C# 12 will come with some interesting features for developers.
Primary Constructors
As the documentation states:
You can now create primary constructors in any class and struct. Primary constructors are no longer restricted to record types.
Primary constructors in C# 12 are loosely inspired by Scala language. The idea is quite useful. When you declare a class with a constructor accepting parameters, you have to create properties as well, and assign all these constructor parameters to properties.
A primary constructor does it for you. You just define your constructor parameters, and automatically they become parameters of the class: