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

后端 未结 6 1977
佛祖请我去吃肉
佛祖请我去吃肉 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:38

    One, since they're static, the compiler will be able to optimize it to a single string at compile time.

    If they had been dynamic, they'd have been optimized to a single call to String.Concat(string, string, string, string).

提交回复
热议问题