As indicated, the reason is that zero concatenation is considered the same as empty string concatenation.
It is worth considering why this behavior is useful.
Typically, there are two reasonable things we can do with a binary operator when one of the operands is null:
- The result is null.
- The operation is no-op, and we are left with another operand.
It makes sense, for example, that ((int?)null) + 3 leads to null , and usually either it will be the most useful result, or one that we will consciously protect (that is, we will add code to catch the null value of the case explicitly )
But there are two reasons not to do this when concatenating strings.
Firstly, it should be borne in mind that since concatenation does not mean arithmetic calculation, but the combination of two things together, what is the most reasonable result of sticking a zero value to the beginning or end of something? It is easy to make it do nothing and not return null.
Secondly, in practice there will be fewer cases where we want a + b + c + d with strings to return null if any of them is null than when we did not.
And from this it makes sense to consider null as an empty string in concatenations. Based on this, (string)null + (string)null leads to "" , because we have no special case for concatenating too zeros.
This special case can be added, but then the property x + "" == x + null will no longer be fulfilled, which may lead to some strange cases.
Jon Hanna Feb 14 '14 at 16:54 2014-02-14 16:54
source share