Primary Constructors (C# 12 Syntactic Sugar): A Guide
With the introduction of .Net8 and C#12 in November of 2023, there were many significant changes to .NET and C#. One such feature in C#12 is the concept of a Primary Constructor. A Primary constructor replaces a class constructor with parameters on the class. The parameters for the constructor are available anywhere in the body of the class. With Primary Constructor: It is important to note that the class parameters are not fields of the class. The following code will not compile if used in SimpleClass, as foo and bar are not available on “this”. Using a Primary Constructor can significantly improve the readability of your code, as it does away with a lot … Read more