你如何使用逐字字符串插值? [英] How do you use verbatim strings with interpolation?

查看:235
本文介绍了你如何使用逐字字符串插值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#6有一个新功能:插入字符串

In C#6 there is a new feature: interpolated strings.

这让你把表达式直接转换成代码,而不是依赖于指标:

These let you put expressions directly into code, rather than relying on indexes:

string s = string.Format("Adding \"{0}\" and {1} to foobar.", x, this.Y());



变成了:

Becomes:

string s = $"Adding \"{x}\" and {this.Y()} to foobar.";



不过,我们有很多遇到像这样使用逐字字符串(主要是SQL语句)多行字符串

However, we have a lot of strings across multiple lines using verbatim strings (mainly SQL statements) like this:

string s = string.Format(@"Result...
Adding ""{0}"" and {1} to foobar:
{2}", x, this.Y(), x.GetLog());



还原这些常规的字符串看起来凌乱:

Reverting these to regular strings seems messy:

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



我如何使用这两种逐字记录和插串在一起呢?

How do I use both verbatim and interpolated strings together?

推荐答案

您可以同时适用 $ @ 前缀相同的字符串:

You can apply both $ and @ prefixes to the same string:

string s = $@"Result...
Adding ""{x}"" and {this.Y()} to foobar:
{x.GetLog()}";

这篇关于你如何使用逐字字符串插值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆