String constants
Just use the + operator and break the line up into readable lines. The compiler will pick up the strings as constants and combine them at compile time. See the MSDN C # Programming Guide here .
eg.
const string myVeryLongString = "This is the opening paragraph of my long string. " + "Which is split over multiple lines to improve code readability, " + "but is in fact, just one long string.";
IL_0003: ldstr "This is the opening paragraph of my long string. Which is split over multiple lines to improve code readability, but is in fact, just one long string."
String variables
Please note that when using string interpolation to substitute values ββinto your string, the $ character must precede each line where substitution should be performed:
var interpolatedString = "This line has no substitutions. " + $" This line uses {count} widgets, and " + $" {CountFoos()} foos were found.";
However, this has a negative performance due to multiple calls to string.Format and possible string concatenation (marked with *** )
IL_002E: ldstr "This line has no substitutions. " IL_0033: ldstr " This line uses {0} widgets, and " IL_0038: ldloc.0 // count IL_0039: box System.Int32 IL_003E: call System.String.Format *** IL_0043: ldstr " {0} foos were found." IL_0048: ldloc.1 // CountFoos IL_0049: callvirt System.Func<System.Int32>.Invoke IL_004E: box System.Int32 IL_0053: call System.String.Format *** IL_0058: call System.String.Concat ***
Although you can use $@ to provide a single line and avoid performance issues if the space doesn't fit inside {} (which looks weird, IMO), this has the same problem as Neil Knight's answer, as it will include any spaces in line breaks:
var interpolatedString = $@"When breaking up strings with `@` it introduces <- [newLine and whitespace here!] each time I break the string. <- [More whitespace] {CountFoos()} foos were found.";
The spaces you enter are easy to see:
IL_002E: ldstr "When breaking up strings with `@` it introduces <- [newLine and whitespace here!] each time I break the string. <- [More whitespace] {0} foos were found."
An alternative is to return to string.Format . Here, the format string is the only constant according to my original answer:
const string longFormatString = "This is the opening paragraph of my long string with {0} chars. " + "Which is split over multiple lines to improve code readability, " + "but is in fact, just one long string with {1} widgets.";
And then evaluated as such:
string.Format(longFormatString, longFormatString.Length, CountWidgets());
However, this can be difficult to maintain, given the possible separation between the format string and the substitution tokens.