Today I was reading about the "unassigned variable" detection of the C# compiler. If you write this code:

int x;
Console.WriteLine(x);
The compiler generates an error: error CS0165: Use of unassigned local variable 'x'. Naturally, I wanted to see how this feature fails, and not surprisingly:

int x;
int y = 1;

if (y == 1)
    x = 9;

Console.WriteLine(x);
Generates the same error. This was expected, of course - since the compiler will have to actually run the program to test for the "unassigned" condition in the general case (which is what happens, kind of, in dynamic interpreted languages like Ruby - where runtime and compile time refer to the same process). Still, this error in C# is useful, since it can probably detect 99% of the real-world cases.

Comments

comments powered by Disqus