Introducing C# 10: Structs parameterless constructor and instance field initializer
Introduction
I’m going to talk in this post about an important feature of C# 10 that has gone rather unnoticed. In addition to record support on structs, C# 10 now allows what was previously only possible on classes: parameterless constructors
Syntax
The operation of constructors without parameters still differs on structs with C# 10 compared to classes. A parameterless constructor cannot be used without field initializers (it’s not really true, I’ll show you further). However, like classes, it is possible, like classes, to use field initializers without a parameterless constructor. The following is legal:
The following is not:
Note that on structs, parameterless constructors must be declared public, declaring them private or internal will generate an error at compilation.
If you still want to use a parameterless constructor with no field initializer, in fact, it is possible, just initialize the unassigned field in the constructor as follows:
This syntax described since the beginning of the section applies to any kind of struct: struct, readonly struct, ref struct, record struct. Keep in mind that the syntax of readonly structs is quite different: readonly modifier must be applied on properties AND you have to remove the get keyword. The following is a legal declaration of a readonly struct:
The special case of the record struct
It is possible to combine primary constructors, constructors without parameters, constructors with parameters, and fields initializers. There are many possibilities, here are some examples below:
To finish, I would like to notice there is probably a mistake in the Microsoft documentation, the following example (R1). Microsoft says “‘struct’ with field initializers must include an explicitly declared constructor”. After trying the code compile and can be executed without any issue. Once I get a response from Microsoft I’ll update my post. If you want to check the documentation you can find it here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/parameterless-struct-constructors#record-struct
Conclusion
As the record struct is a pretty huge feature, I have separated this post from my previous one here: Introducing C# 10: Record struct – Anthony Giretti’s .NET blog. This post introduced also parameterless constructors and fields initializers I haven’t been through all the behavior of this feature, for example I haven’t addressed the default() expression, the new() instruction etc… but you an check them here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/parameterless-struct-constructors#default-expression