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

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

    Just one. The C# compiler will fold string constants and hence it essentially compiles down to

    String result = "1234";
    

提交回复
热议问题