rbwhitaker / CSharpPlayersGuideEarlyAccess

A place to track issues with the C# Player's Guide for patches and future editions
19 stars 0 forks source link

Struct initialization #642

Closed rbwhitaker closed 1 year ago

rbwhitaker commented 2 years ago

Structs have always been limited in a bunch of ways. Some of that is a result of them just being different from classes. But they decided to make structs a little smarter in this update, and I think it will make explaining them easier as well.

Classes have always been zero-initialized. Structs have not. The compiler would always go way out of its way to make sure you initialized all of your fields and auto-properties. But since the compiler was always able to detect that you've failed to initialize something, why not just let it zero initialize it instead? There are performance things in there to not do it unnecessarily, but the end effect is that they work more consistently (and with less explanation) than classes.

I suspect this will make the description of structs simpler, though maybe there is some nuance that is not obvious to me right now...

rbwhitaker commented 1 year ago

I've reviewed the book carefully, and I don't think there is a need to address this change in the book. The book never mentions that constructors must initialize all fields, though perhaps it should have before. Now, that is no longer a problem, because they can just be zero-initialized. So whatever should have been done is no longer relevant. I'm going to close this out, since I don't anticipate any additional book changes for it.