Because you can't call a method on a null
reference in any case, so the compiler doesn't allow it. In your first case, the compiler will pre-compile the string constant to "xyz890"
so it is a legal assignment.
In general, the +
operator for string
compiles to String.Concat(s1, s2, ...)
which supports null
arguments.
To be more precise on the second case, the actual problem isn't that it's a null pointer, it's that there is no type to bind the method to. As pointed out in other comments, you can cast null
to any reference type and it compiles just fine:
((string)null).ToString(); // compiles, but fails at run-time.