Best way to break long strings in C# source code

后端 未结 10 1473
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 18:47

I am wondering what is the \"best practice\" to break long strings in C# source code. Is this string

\"string1\"+
\"string2\"+
\"string3\"

con

10条回答
  •  独厮守ぢ
    2021-02-03 19:32

    StringBuilder is a good way to go if you have many (more than about four) strings to concatenate. It's faster.

    Using String.Concat in you example above is done at compile time. Since they are literal strings they are optimized by the compiler.

    If you however use variables:

    string a = "string1";
    string b = "string2";
    string c = a + b;
    

    This is done at runtime.

提交回复
热议问题