Basically, you ask why the compiler cannot determine the type at the first assignment, and not in the declaration, right? Consider the following:
var x; if (DateTime.Now.Hour > 12) { x = 100; } else { x = "Hello"; }
What should the compiler do? The if statement can only be evaluated at run time, and not at compile time, when the type needs to be identified. But at compile time, it can still be either an int or a string.
source share