Simple ... because the compiler insists that you initialize local variables before using them. This prevents a whole category of errors related to initialization failure.
If you look at IL generated from the following few statements:
int x; Console.WriteLine("hello"); int y=5; x=6; Console.WriteLine(x+y);
You will see the following:
//notice no ops related to x prior to console.writeline IL_0000: ldstr "hello" IL_0005: call System.Console.WriteLine IL_000A: ldc.i4.5 IL_000B: stloc.1 //create/store y IL_000C: ldc.i4.6 IL_000D: stloc.0 //x is really only created right here IL_000E: ldloc.0 IL_000F: ldloc.1 IL_0010: add IL_0011: call System.Console.WriteLine
if you see the value for x before storing it in IL, then this is a debugger trick.
spender
source share