C# 如何使用带插值的逐字字符串?

C# 如何使用带插值的逐字字符串?,c#,string-interpolation,c#-6.0,verbatim-string,C#,String Interpolation,C# 6.0,Verbatim String,在C#6中有一个新特性:插值字符串。通过这些,您可以将表达式直接放入代码中 与其依赖索引,不如: string s = string.Format("Adding \"{0}\" and {1} to foobar.", x, this.Y()); 上述内容变为: string s = $"Adding \"{x}\" and {this.Y()} to foobar."; 但是,我们有许多跨多行使用逐字字符串(

在C#6中有一个新特性:插值字符串。通过这些,您可以将表达式直接放入代码中

与其依赖索引,不如:

string s = string.Format("Adding \"{0}\" and {1} to foobar.", x, this.Y());
上述内容变为:

string s = $"Adding \"{x}\" and {this.Y()} to foobar.";
但是,我们有许多跨多行使用逐字字符串(主要是SQL语句)的字符串,如下所示:

string s = string.Format(@"Result...
Adding ""{0}"" and {1} to foobar:
{2}", x, this.Y(), x.GetLog());
将这些字符串恢复为常规字符串似乎很混乱:

string s = "Result...\r\n" +
$"Adding \"{x}\" and {this.Y()} to foobar:\r\n" +
x.GetLog().ToString();

如何同时使用逐字和插入字符串?

您可以将
$
@
前缀应用于同一字符串:

string s = $@"Result...
Adding ""{x}"" and {this.Y()} to foobar:
{x.GetLog()}";
自创建以来,插入的逐字字符串必须以标记
$@
开头