The following code is illegal:
public struct MyStruct { public MyStruct(int a, int b) { this.a = a; this.b = b; } public int a; public int b; }
How it should be done is obvious:
MyStruct defaultValue = default(MyStruct) //or new MyStruct(); ... if (foo != defaultValue) //compiler is happy
But the following is also allowed (I did not know this and accidentally stumbled upon it):
MyStruct defaultValue; defaultValue.a = 0; defaultValue.b = 0; ... if (foo != defaultValue)
I assume that the compiler checks that all struct fields have been initialized and therefore allows this code to be compiled. However, I am confused how the rest of the language works. After all, you mostly use an unrecognized variable in C # way to view things.
Everything becomes even more confusing if you consider the following code:
public struct MyStruct { public MyStruct(int a, int b) { this.a = a; this.b = b; this.c = (a + b).ToString(); } public int a; public int b; internal string c; }
The following code is illegal because in this case we did not assign all the visible fields:
MyStruct defaultValue; defaultValue.a = 0; defaultValue.b = 0; ... if (foo != defaultValue) //use of unassigned variable...
The keyword is visible here, because if MyStruct to be defined in the referenced assembly, then c not displayed, and the compiler will not complain, and the previous code will be absolutely correct. Confuse again.
Can someone explain why it is allowed to initialize a struct in C # in this way? Why not completely ban it, so there is a more unified experience of using any type of value in the language?
EDIT 1 : I made a mistake in the last example. The compiler will be happy only if the non-visible field is of type reference . Even more confusing. Is this last case a known error in the compiler or is there a reasonable reason for it to work the way it is done?
The last example is changed in the actual case.
EDIT 2 . I am still stunned by how value type initialization works. Why, for example, the following is not allowed:
struct MyStruct { public int A { get; set; }
As I see it, there is no doubt that all MyStruct fields MyStruct initialized. I can understand why this will not be allowed if the properties were not automatically implemented, since it is possible that the setters do not guarantee that all fields are set. But in automatically implemented properties, the compiler knows with 100% certainty that the fields will be set if the properties (after all of its code that the compiler generates for you).
Finally, a small theoretical case, which, obviously, has no practical application:
struct MyUselessStruct { } MyUselessStruct defaultValue; Console.WriteLine(defaultValue.ToString());
Then why is this not allowed:
Object object; if (object == null) ....
I find both cases to be similar in concept, and I expect them both to work the same in C #. I still donβt understand why this seemingly useless differentiation in how value type variables can be initialized and what it is for practical use (besides the inconsistencies that I explained in the first part of my question)