4 ms·
I really like the C# object initializer syntax, and it looks like the next version might get rid of the last problem I had with it. Right now it does not play n
by fabian2k 4y ago
I really like the C# object initializer syntax, and it looks like the next version might get rid of the last problem I had with it. Right now it does not play nice with nullable types, if a property is declared non-nullable you have to use a constructor or some ugly trick to circumvent the warnings. As far as I understand this will be fixed in .NET 7 (not sure if the decision is final, though) and you can get the full benefit of non-nullable types when using object initializer syntax.
In general there has been quite some effort to allow a programming style in C# with less of the ceremony and verbosity that is often associated with C# and Java. And to me this does make working with C# more pleasant.
- diarrhea 4y agoIf you perhaps happen to know or have a pointer: how would that work in C#? A field not initialised will be null, and if it’s non-nullable… what’s its value going to be?
- fabian2k 4y agoThe check would be moved to the object initialization, so the compiler would tell you if you use the flexible object initialization syntax and left some non-nullable properties uninitialized.