在C#6中有一个新功能:插值字符串.
这些允许您将表达式直接放入代码中,而不是依赖于索引:
string s = string.Format("Adding \"{0}\" and {1} to foobar.", x, this.Y());
Run Code Online (Sandbox Code Playgroud)
变为:
string s = $"Adding \"{x}\" and {this.Y()} to foobar.";
Run Code Online (Sandbox Code Playgroud)
但是,我们在使用逐字符串(主要是SQL语句)的多行中有很多字符串,如下所示:
string s = string.Format(@"Result...
Adding ""{0}"" and {1} to foobar:
{2}", x, this.Y(), x.GetLog());
Run Code Online (Sandbox Code Playgroud)
将这些恢复为常规字符串似乎很麻烦:
string s = "Result...\r\n" +
$"Adding \"{x}\" and {this.Y()} to foobar:\r\n" +
x.GetLog().ToString();
Run Code Online (Sandbox Code Playgroud)
如何同时使用逐字和插值字符串?
我有代码在注册时向用户发送电子邮件:
await UserManager.SendEmailAsync(2, "Confirm your account",
"Please confirm your account by clicking this link: <a href=\"www.cnn.com\">link</a>");
Run Code Online (Sandbox Code Playgroud)
这有效,但我想做更高级的事情,我看到很多模板.但是,所有模板都至少有100行,每行后都有换行符.这是我尝试添加一个新行时的示例.
await UserManager.SendEmailAsync(2, "Confirm your account",
"Please confirm your account by clicking this link:
<a href=\"www.cnn.com\">link</a>");
Run Code Online (Sandbox Code Playgroud)
一旦我有一个新的行,然后我收到一条消息,说我不能在常量中包含一个新行.
任何人都可以提出另一种方法,我可以包括这样做吗?