struct MYSTRUCT
{
public int x;
public int y;
public int z;
}
MYSTRUCT s;
s.x = 2;
s.y = 4;
Console.WriteLine(s); //error on this line
This generates a compiler error
error CS0165: Use of unassigned local variable 's'
It raises a couple of questions - firstly since a struct is a value type and non-nullable, why does s
need to be initialized? Secondly, why is assigning to s.x
and s.y
OK and only the final line generates the error?
I'm not asking how to fix my code, it just made me interested why a struct must be assigned/initialized as a value type?
(note: MYSTRUCT
only has simple byte
/int
members)