I am surprised that I can't initialize my fields in structs, why is it like that? Like:
struct MyStruct { private int a = 90; } but it's a complie time error. I don't know why it's a problem? Please explain this to me.
I am surprised that I can't initialize my fields in structs, why is it like that? Like:
struct MyStruct { private int a = 90; } but it's a complie time error. I don't know why it's a problem? Please explain this to me.
The reason is mainly performance. Consider the following,
var a = new MyStruct[1000]; If C# allowed initialization of fields in a struct then the initialization would have to be performed 1000 times, once for each element in the array. C# wanted to avoid such kinds of implicit behavior as might be found in other languages.
default(TheSaidStruct) instead of just zero filling.That's because your assignment is actually transformed by the compiler to be done in the default constructor. But C# structs don't have default constructors, as you can see in the link posted by Kent Boogaart.