如何在C#6.0字符串插值中输入引号字符

SAJ*_*SAJ 4 c# string-interpolation c#-6.0

特定

IDictionary<string, string> x;
Run Code Online (Sandbox Code Playgroud)

以前你可以做(​​作为带引号的参数代码示例):

string.Format("{0}", x["y"]);
Run Code Online (Sandbox Code Playgroud)

格式化C#6.0字符串插值的正确方法是什么?

$"{x["y"]}"   // compiler error due to the quotes on the indexer value
              // UPDATE:  actually does work, must have had another typo I missed
Run Code Online (Sandbox Code Playgroud)

将引号转义为

\"
Run Code Online (Sandbox Code Playgroud)

不行,做

 var b = "y";
 ...
 $"{x[b]}"
Run Code Online (Sandbox Code Playgroud)

看起来很尴尬.

Nas*_*ine 7

这对我有用:

var dictionary= new Dictionary<string, string>();
dictionary.Add("x","value of x");
Console.WriteLine($"x is {dictionary["x"]}");
Run Code Online (Sandbox Code Playgroud)

确保您的项目设置为使用C#语言级别的6.0版本(这是VS2015上的默认选项).

编辑:你也可以在这里试试.(请务必查看"C#6.0 Beta").