VS 2015中字符串插值的最终格式是什么?

cit*_*kid 34 c# string-interpolation c#-6.0

我不能让字符串插值工作.MS发现的最新消息是

http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx

然而所有这一切都表示,目前没有工作.有人知道字符串插值是否进入VS 2015?有没有关于它的文件?你举个例子吗?

例如,这些格式都不起作用(编辑):

int i = 42;
var s = "\{i}";  // correction after jon's answer: this works!
var s = $"{i}";  // compiler error
var s = "{{i}}"; // no interpolation
Run Code Online (Sandbox Code Playgroud)

编辑关于VS 2015 CTP 6(20.4.2015)

最终版本是

var s = $"{i}"
Run Code Online (Sandbox Code Playgroud)

目前的Resharper版本ReSharper 9.1.20150408.155143也支持

Jon*_*eet 50

你的第一种形式在VS2015预览工作:

int i = 42;
var s = "\{i}";
Run Code Online (Sandbox Code Playgroud)

那编译并为我跑了.ReSharper抱怨道,但这是另一回事.

对于C#的最终版本,它是:

var s = $"{i}";
Run Code Online (Sandbox Code Playgroud)

  • 确认在2015年1月16日发布的VS 2015 CTP中格式发生了变化.`var s = $"{i}";` (2认同)

i3a*_*non 14

字符串插值正在进入VS 2015.它的最新语法(尚未准备好进行预览,但已进入VS2015 CTP5)是这样的:

string s = $"{i}";
Run Code Online (Sandbox Code Playgroud)

它还支持IFormattable使用FormattableString类的结果:

IFormattable s = $"{i}";
Run Code Online (Sandbox Code Playgroud)

最新的设计文档在这里:C#的字符串插值(v2)

您可以使用http://tryroslyn.azurewebsites.net上的最新Roslyn版本在线查看.这是具体的例子.