行在C#中继续字符

我们有一个包含很长string的variables的unit testing。

问题是如何在代码中编写这些代码,而不存在换行符问题或代码难以阅读。

在VB中有一行继续字符,在C#中有一个equivilant?

C#将允许你有一个string拆分成多行,这个术语被称为verbatim literal

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

如果您正在寻找VB的替代方法,请使用+join您的行。

string常量

只需使用+运算符并将string分解为可读的行。 编译器会提取string是不变的,并在编译时连接它们。 请参阅MSDN C#编程指南。

例如

 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."

stringvariables

请注意,使用string插值将值replace为您的string时, $字符需要在需要进行replace的行之前:

 var interpolatedString = "This line has no substitutions. " + $" This line uses {count} widgets, and " + $" {CountFoos()} foos were found."; 

但是,这会对多次调用string.Format和string的最终串联(用***标记)产生负面的性能结果,

 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 *** 

尽pipe你可以使用$@来提供一个单一的string,并避免性能问题, 除非空格被放在{} (这看起来很古怪,IMO),这与Neil Knight的答案有相同的问题,因为它将包括任何空格在线故障:

 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."; 

注入的空白很容易被发现:

 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." 

另一种方法是恢复到string.Format 。 在这里,根据我的初始答案,格式化string是一个单一的常量:

 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."; 

然后评估如下:

 string.Format(longFormatString, longFormatString.Length, CountWidgets()); 

但是,考虑到格式化string和replace令牌之间的潜在分离,这仍然可能是棘手的。

你可以使用逐字文字:

 const string test = @"Test 123 456 "; 

但是第一行的缩进是棘手的/丑陋的。

 @"string here that is long you mean" 

但要小心,因为

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

它也逃避了string中的东西

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

有关

  • 在C#中,variables名之前的@符号是什么意思?
  • MSDNstring参考

您必须使用以下方法之一:

  string s = @"loooooooooooooooooooooooong loooooong long long long"; string s = "loooooooooong loooong" + " long long" ; 

如果你声明不同的variables,那么使用下面的简单方法

 Int salary=2000; String abc="I Love Pakistan"; Double pi=3.14; Console.Writeline=salary+"/n"+abc+"/n"+pi; Console.readkey();