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

后端 未结 4 494
陌清茗
陌清茗 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:20

    the + operator for strings are just shorthand for string.Concat which simply turns null arguments into empty strings before the concatenation.

    Update:

    The generalized version of string.Concat:

    public static string Concat(params string[] values)
    {
        int num = 0;
        if (values == null)
        {
            throw new ArgumentNullException("values");
        }
        string[] array = new string[values.Length];
        for (int i = 0; i < values.Length; i++)
        {
            string text = values[i];
            array[i] = ((text == null) ? string.Empty : text);
            num += array[i].Length;
            if (num < 0)
            {
                throw new OutOfMemoryException();
            }
        }
        return string.ConcatArray(array, num);
    }
    
    0 讨论(0)
  • 2021-02-05 00:32

    Here is what your code gets compiled to

    string sample = null;
    sample += "test";
    

    is compiled to this IL code:

    .entrypoint
      // Code size       16 (0x10)
      .maxstack  2
      .locals init ([0] string sample)
      IL_0000:  nop
      IL_0001:  ldnull
      IL_0002:  stloc.0
      IL_0003:  ldloc.0
      IL_0004:  ldstr      "test"
      IL_0009:  call       string [mscorlib]System.String::Concat(string,
                                                                  string)
      IL_000e:  stloc.0
      IL_000f:  ret
    

    And String.Concat takes care of NULL string.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 00:45

    it is because

    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.

    From How to: Concatenate Multiple Strings (C# Programming Guide)

    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.

    From Addition operator

    0 讨论(0)
提交回复
热议问题