string s1;
string s2 = null;
if (s1 == null) // compile error
if (s2 == null) // ok
I don’t really understand why the explicit assignment is needed. Whats the difference between a null variable and an unassigned variable? I always assumed that unassigned variables were simply assigned as null by the runtime/compiler anyway. If they’re not null, then what are they?
Unassigned members are automatically initialized to their default values (which is the null reference in the case for
string).Unassigned local variables are not assigned any value and trying to access a possibly unassigned variable will give a compile error.