In C#, what's the best way to spread a single-line string literal across multiple source lines?

前端 未结 7 1540
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 18:19

Suppose that you have a lengthy string (> 80 characters) that you want to spread across multiple source lines, but don\'t want to include any newline characters.

One

7条回答
  •  借酒劲吻你
    2020-12-30 19:07

    You could use multiple consts and then combine them into one big string:

    const string part1 = "part 1";
    const string part2 = "part 2";
    const string part3 = "part 3";
    string bigString = part1 + part2 + part3;
    

    The compiler will "fold" these constants into one big string anyway, so there is no runtime cost at all to this technique as compared to your original code sample.

    There are a number of advantages to this approach:

    1. The substrings can be easily reused in other parts of the application.
    2. The substrings can be defined in multiple files or types, if desired.

提交回复
热议问题