From MSDN:
When you create a struct object using the new operator, it gets
created and the appropriate constructor is called. Unlike classes,
structs can be instantiated without using the new operator. In such a
case, there is no constructor call, which makes the allocation more
efficient. However, the fields will remain unassigned and the object
cannot be used until all of the fields are initialized.
From MSDN:
Compiler Error CS0170: Use of possibly unassigned field 'field'. A
field in a structure was used without first being initialized. To
solve this problem, first determine which field was uninitialized and
then initialize it before you try to access it.
From MSDN:
Compiler Error CS0165: Use of unassigned local variable 'name'. The C# compiler does not allow the use of uninitialized variables. If
the compiler detects the use of a variable that might not have been
initialized, it generates compiler error CS0165.
This is wrong:
I always thought all fields of a struct have to be assigned to before one can access data members of the struct
The correct one is:
All fields of a struct have to be assigned to before one can access the struct.
Color c;
c.B = 0xFF;
int b = c.B; // Okay. You have assigned B
int r = c.R; // Error CS0170! Use of possibly unassigned field
Color cc = c; // Error CS0165! Use of unassigned local variable. The object cannot be used until all of the fields are initialized