34

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

pauljz
  • 10,803
  • 4
  • 28
  • 33
citykid
  • 9,916
  • 10
  • 55
  • 91

2 Answers2

50

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}";
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • looks like the promised intellisense is not working yet. – citykid Jan 04 '15 at 15:46
  • 1
    Correct; here's the latest feature descriptions PDF for reference (see Section 5): https://www.codeplex.com/Download?ProjectName=roslyn&DownloadId=930852 – Gigi Jan 04 '15 at 15:59
  • 2
    Confirmed that the format has changed in the VS 2015 CTP released on Jan 16 2015. `var s = $"{i}";` – p.campbell Jan 20 '15 at 16:18
  • I might put the most current version at the top for those of us who don't read too good and look elsewhere too soon! (evidenced by the highest upvote before being marked duplicate at https://stackoverflow.com/questions/29154868/new-c-sharp-6-0-string-interpolation-statements-dont-compile/29154971#29154971) –  Aug 08 '15 at 20:46
  • I'm on vs2015 using the latest R# and targetting framework 4.5.1 and string interpolation is being suggested by R# and its compiling/working as well? It only then fails on the build server (vs 2013 on teamcity) - ?? – wal Feb 07 '16 at 12:19
  • @wal: Well yes, it needs C# 6, which VS2013 doesn't support. You should upgrade your build server to support the version of the language you're using... – Jon Skeet Feb 07 '16 at 12:34
  • @JonSkeet yes true (build server shld match dev env) however it is a curiosity that vs2015 is even compiling this given the target is .net4.5 and string interpolation is a `c#6.0` construct? i am just assuming it is just syntax sugar that VS then converts to something that can be compiled under .net4 – wal Feb 08 '16 at 01:59
  • @wal: Yes, that's the case for all the C# 6 features, although string interpolation can use a type called FormattableString if it's present. – Jon Skeet Feb 08 '16 at 06:06
  • @JonSkeet For multiline strings I used to write strings starting with @. How should I write now? Neither $@ nor @$ works now :( – Andrius Naruševičius Apr 03 '16 at 12:58
  • @AndriusNaruševičius: `$@` should be fine. – Jon Skeet Apr 03 '16 at 13:31
14

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.

i3arnon
  • 113,022
  • 33
  • 324
  • 344