Why is it Valid to Concatenate Null Strings but not to Call “null.ToString()”?

The reason for first one working:

From MSDN:

In string concatenation operations,the C# compiler treats a null string the same as an empty string, but it does not convert the value of the original null string.

More information on the + binary operator:

The binary + operator performs string concatenation when one or both operands are of type string.

If an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object.

If ToString returns null, an empty string is substituted.

The reason of the error in second is:

null (C# Reference) – The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables.

Leave a Comment

File not found.