costanti stringa
Basta usare l'operatore +
e rompere la corda fino in linee leggibile. Il compilatore capirà che le stringhe sono costanti e le concatena in fase di compilazione. Vedere la Guida alla programmazione MSDN C# here.
ad es.
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."
Variabili di stringa
notare che quando utilizzate interpolazione di stringhe per sostituire i valori nella stringa, che il carattere $
deve precedere ogni riga dove una sostituzione deve essere fatto:
var interpolatedString =
"This line has no substitutions. " +
$" This line uses {count} widgets, and " +
$" {CountFoos()} foos were found.";
Tuttavia, questo ha il performance negativa conseguenza di più chiamate a string.Format
ed eventuale concatenazione delle stringhe (contrassegnati con ***
)
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 ***
Anche se è possibile usare sia [email protected]
per fornire una singola stringa e di evitare le prestazioni questioni, unless the whitespace is placed inside {}
(che sembra strano, IMO), questo ha lo stesso problema come la risposta di Neil Knight, in quanto includerà qualsiasi spazi bianchi nei guasti di linea:
var interpolatedString = [email protected]"When breaking up strings with `@` it introduces
<- [newLine and whitespace here!] each time I break the string.
<- [More whitespace] {CountFoos()} foos were found.";
La spaziatura iniettato è facile da individuare:
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."
Un'alternativa è di ripristinare string.Format
. Qui, la stringa di formattazione è un singolo costante come per la mia prima risposta:
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.";
E poi valutato come tale:
string.Format(longFormatString, longFormatString.Length, CountWidgets());
Tuttavia, questo può ancora essere difficile da mantenere dato il potenziale separazione tra la formattazione stringa e i token di sostituzione.
creare un array, quindi unirlo. – leppie
Possibile duplicato di [Multiline String Literal in C#] (http://stackoverflow.com/questions/1100260/multiline-string-literal-in-c-sharp) –