Memory usage of concatenating strings using interpolated vs “+” operator

后端 未结 4 889
无人共我
无人共我 2021-01-01 15:36

I see the benefit of using interpolated strings, in terms of readability:

string myString = $\"Hello { person.FirstName } { person.LastName }!\"
4条回答
  •  被撕碎了的回忆
    2021-01-01 16:07

    Because strings in c# are immutable that's why same memory is used again and again so it does not impact memory much but in terms of performance you are actually differentiating between String.Format and String.Concat because at compile time your code will be like this

      string a = "abc";
      string b = "def";
    
      string.Format("Hello {0} {1}!", a, b);
    
      string.Concat(new string[] { "Hello ", a, " ", b, "!" });
    

    there is a whole thread about performance between these two if you are interested String output: format or concat in C#

提交回复
热议问题