Linear continuation character in C #

We have a unit test with a variable that contains a very long string.

The question is how to write this to the code, without problems with line breaks or that the code is difficult to read.

In VB there is a line continuation character, is there equivilant in C #?

+58
c #
Nov 03 '10 at
source share
7 answers

C # will allow you to have a string split across multiple lines, a term called verbatim literal :

 string myString = @"this is a test to see how long my string can be and it can be quite long"; 

If you are looking for the & _ alternative from VB, use + to concatenate your strings.

+58
Nov 03 '10 at 10:41
source share
β€” -

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.

+50
Nov 03 '10 at 10:40
source share
 @"string here that is long you mean" 

But be careful because

 @"string here and space before this text means the space is also a part of the string" 

He also avoids things in line

 @"c:\\folder" // c:\\folder @"c:\folder" // c:\folder "c:\\folder" // c:\folder 

Similar

+6
Nov 03 '10 at 10:37
source share

You can use literal literals:

 const string test = @"Test 123 456 "; 

But the indentation of the 1st line is complicated / ugly.

+5
Nov 03 '10 at 10:38
source share

You must use one of the following methods:

  string s = @"loooooooooooooooooooooooong loooooong long long long"; string s = "loooooooooong loooong" + " long long" ; 
0
Nov 03. '10 at 10:38
source share

In his excellent answer, StuartLC cites the answer to a related question and mentions that placing newlines inside the {expression} interpolated string "looks weird." Most will agree, but the unpleasant effect of the source code can be somewhat mitigated - and without any consequences at runtime - by using the selected blocks {expression} , which are converted to default(String) , that is, null (and, in particular, not String.Empty ).

(Although secondary) the point is not to spoil or spoil your actual content of the expression, instead using the allocated token for this purpose. So if you declare a constant, for example:

 const String more = null; 

... then a line that may be too long to view in the source code, for example ...

 var s1 = $"one: {99 + 1} two: {99 + 2} three: {99 + 3} four: {99 + 4} five: {99 + 5} six: {99 + 6}"; 

... instead, you can write like this.

 var s2 = $@"{more }one: {99 + 1} {more }two: {99 + 2} {more }three: {99 + 3} {more }four: {99 + 4} {more }five: {99 + 5} {more }six: {99 + 6}"; 

Or perhaps you prefer a different β€œweird” approach to the same thing:

 // elsewhere: public const String Ξ = null; // Unicode '\u039E', Greek 'Capital Letter Xi' // anywhere: var s3 = $@"{ Ξ}one: {99 + 1} { Ξ}two: {99 + 2} { Ξ}three: {99 + 3} { Ξ}four: {99 + 4} { Ξ}five: {99 + 5} { Ξ}six: {99 + 6}"; 

All three examples return the same string at run time, which in this case is on the same line:

one: 100 two: 101 three: 102 four: 103 five: 104 six: 105

As Stuart suggested, IL performance is preserved in both of these examples without using + to concatenate strings. Although the longer format string in my new example is actually stored in IL, and therefore in your executable file, the empty placeholders it refers to are not initialized, and there are no excessive concatenations or function calls at run time. For comparison, here is the IL for the two examples above.

IL for the first example

 ldstr "one: {0} two: {1} three: {2} four: {3} five: {4} six: {5}" ldc.i4.6 newarr object dup ldc.i4.0 ldc.i4.s 100 box int32 stelem.ref dup ldc.i4.1 ldc.i4.s 101 box int32 stelem.ref dup ldc.i4.2 ldc.i4.s 102 box int32 stelem.ref dup ldc.i4.3 ldc.i4.s 103 box int32 stelem.ref dup ldc.i4.4 ldc.i4.s 104 box int32 stelem.ref dup ldc.i4.5 ldc.i4.s 105 box int32 stelem.ref call string string::Format(string, object[]) 

IL for the second example

 ldstr "{0}one: {1} {2}two: {3} {4}three: {5} {6}four: {7} {8}five: {9} {10}six: {11}" ldc.i4.s 12 newarr object dup ldc.i4.1 ldc.i4.s 100 box int32 stelem.ref dup ldc.i4.3 ldc.i4.s 101 box int32 stelem.ref dup ldc.i4.5 ldc.i4.s 102 box int32 stelem.ref dup ldc.i4.7 ldc.i4.s 103 box int32 stelem.ref dup ldc.i4.s 9 ldc.i4.s 104 box int32 stelem.ref dup ldc.i4.s 11 ldc.i4.s 105 box int32 stelem.ref call string string::Format(string, object[]) 
0
Jul 08 '19 at 8:03
source share

If you specified different variables, use the following simple method:

 Int salary=2000; String abc="I Love Pakistan"; Double pi=3.14; Console.Writeline=salary+"/n"+abc+"/n"+pi; Console.readkey(); 
-one
Oct 13 '14 at 4:30
source share



All Articles