Why C# compiler does not allow you to compile this:
int a;
Console.WriteLine(a);
but does allow you to compile:
MyStruct a;
Console.WriteLine(a);
where MyStruct is defined as:
struct MyStruct
{
}
Update: in the firsts case the error is:
Error 1 Use of unassigned local
variable ‘a’
C# does not allow reading from uninitialized locals. Here’s an extract from language specification that applies in this context:
Clearly, since your struct has no fields, this isn’t an issue; it is considered definitely assigned. Adding a field to it should break the build.
On a somewhat related note: