Why does concatenation of two null strings result in an empty string?

I just saw a weird result, while I tried to combine two lines null: it returns empty! I can’t imagine if he has any kind of utility or why this is happening.

Example:

string sns = null;
sns = sns + sns;
// It results in a String.Empty

string snss = null;
snss = String.Concat(snss, snss);
// It results in a String.Empty too!

Can someone tell me why it returns String.Emptyinstead null?

+4
source share
2 answers

Here is a snippet from the C # Language Specification, section "Add statement 7.8.4":

String concatenation:

string operator +(string x, string y);
string operator +(string x, object y);
string operator +(object x, string y);

+ . null, . , ToString, object. ToString null, .

+3

Source: https://habr.com/ru/post/1652247/


All Articles