Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the final format for string interpolation in VS 2015?

I can't get string interpolation to work. Last news from MS I found was

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

However all that is said there is not working. Anyone knows if string interpolation made it into VS 2015? Is there any documentation about it? Can one you give an example?

For instance, none of these formats work (edited):

int i = 42; var s = "\{i}";  // correction after jon's answer: this works! var s = $"{i}";  // compiler error var s = "{{i}}"; // no interpolation 

edit about VS 2015 CTP 6 (20.4.2015 )

The final version is

var s = $"{i}" 

also supported by the current Resharper version ReSharper 9.1.20150408.155143

like image 792
citykid Avatar asked Jan 04 '15 15:01

citykid


2 Answers

Your first form did work in the VS2015 Preview:

int i = 42; var s = "\{i}"; 

That compiled and ran for me. ReSharper complained, but that's a different matter.

For the final release of C#, it is:

var s = $"{i}"; 
like image 78
Jon Skeet Avatar answered Sep 18 '22 00:09

Jon Skeet


String interpolation is making it to VS 2015. Its latest syntax (which wasn't ready for the preview, but made it into VS2015 CTP5) is this:

string s = $"{i}"; 

It also supports am IFormattable result using the FormattableString class:

IFormattable s = $"{i}"; 

The latest design documentation is here: String Interpolation for C# (v2)

You can check that online using the latest Roslyn version with http://tryroslyn.azurewebsites.net. Here's the specific example.

like image 24
i3arnon Avatar answered Sep 18 '22 00:09

i3arnon