C#中的行继续字符

Shi*_*iji 59 c#

我们有一个包含很长字符串的变量的单元测试.

问题是如何在代码中编写它,没有换行问题或代码难以阅读.

在VB中有一个行继续字符,在C#中是否存在等价物?

Nei*_*ght 60

C#将允许您将字符串拆分为多行,该术语称为verbatim literal:

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



                    and it can be quite long";
Run Code Online (Sandbox Code Playgroud)

如果您正在寻找& _VB 的替代品,请使用+加入您的行.

  • myString将填充空格.不要这样做. (17认同)
  • 如果您在代码中嵌入SQL字符串,那么这样做是值得的.SQL解析器不关心空白区域,并且在程序和SQL客户端之间传输此类语句时更容易. (9认同)
  • 但想到字节浪费! (3认同)
  • @NH。-可以肯定的是这句话真是太难了。 (2认同)

Stu*_*tLC 50

字符串常量

只需使用+运算符并将字符串分解为人类可读的行.编译器会发现字符串是常量的,并在编译时将它们连接起来.请参阅此处的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.";
Run Code Online (Sandbox Code Playgroud)

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

字符串变量

请注意,当使用字符串插值将值替换为字符串时,该$字符需要位于需要进行替换的每一行之前:

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

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

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 ***
Run Code Online (Sandbox Code Playgroud)

虽然您可以使用$@提供单个字符串并避免性能问题,除非将空白放在里面{}(看起来奇怪,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.";
Run Code Online (Sandbox Code Playgroud)

注入的空白很容易发现:

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."
Run Code Online (Sandbox Code Playgroud)

另一种方法是恢复string.Format.根据我的初始答案,格式化字符串是一个常量:

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.";
Run Code Online (Sandbox Code Playgroud)

然后评估如下:

string.Format(longFormatString, longFormatString.Length, CountWidgets());
Run Code Online (Sandbox Code Playgroud)

然而,考虑到格式化字符串和替换标记之间的潜在分离,这仍然很难维护.


Bru*_*oLM 6

@"string here
that is long you mean"
Run Code Online (Sandbox Code Playgroud)

但要小心,因为

@"string here
           and space before this text
     means the space is also a part of the string"
Run Code Online (Sandbox Code Playgroud)

它也逃脱了字符串中的东西

@"c:\\folder" // c:\\folder
@"c:\folder" // c:\folder
"c:\\folder" // c:\folder
Run Code Online (Sandbox Code Playgroud)

有关


Hen*_*man 5

您可以使用逐字文字:

const string test = @"Test
123
456
";
Run Code Online (Sandbox Code Playgroud)

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