How many String objects will be created when using a plus sign?

后端 未结 6 1976
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 18:07

How many String objects will be created when using a plus sign in the below code?

String result = \"1\" + \"2\" + \"3\" + \"4\";

If it was

6条回答
  •  误落风尘
    2020-11-30 18:46

    I found the answer at MSDN. One.

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

    Concatenation is the process of appending one string to the end of another string. When you concatenate string literals or string constants by using the + operator, the compiler creates a single string. No run time concatenation occurs. However, string variables can be concatenated only at run time. In this case, you should understand the performance implications of the various approaches.

提交回复
热议问题