How does appending to a null string work in C#?

后端 未结 4 496
陌清茗
陌清茗 2021-02-05 00:02

I was surprised to see an example of a string being initialised to null and then having something appended to it in a production environment. It just smelt wrong.

I was

4条回答
  •  感情败类
    2021-02-05 00:41

    The relevant citation should be ECMA-334 §14.7.4:

    String concatenation:

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

    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 operand 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.

提交回复
热议问题