8

After reading the following:

it seemed to me that aside from String Interpolation any project I compiled in VS2015 against .NET 4.51 could use the new C# language features.

However I tried the following code on my dev machine using VS2015 targeting 4.51:

string varOne = "aaa";

string varTwo = $"{varOne}";

if (varTwo == "aaa")
{

}

and not only did I not receive a compiler error, it worked as varTwo contained aaa as expected.

Can someone explain why this is the case as I would not have expected this to work? I am guessing I am missing what FormattableString really means. Can someone give me an example?

Community
  • 1
  • 1
TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • 2
    Perhaps it is smart enough to translate the string to a `String.Format` call? – BradleyDotNET Oct 21 '15 at 00:57
  • 1
    `FormattableString` was introduced in .NET 4.5. – MarcinJuraszek Oct 21 '15 at 01:00
  • @MarcinJuraszek So are you saying i3arnon's comment to Rubenisme is wrong in "Does C# 6.0...."? – TheEdge Oct 21 '15 at 01:12
  • 1
    It probably would work even with 1.0 if compiler could target it (can't due to other features like async) - `String.Format` was there forever (which is what interpolation is compiled into to my understanding) – Alexei Levenkov Oct 21 '15 at 01:29
  • @TheEdge please explain why you "would not have expected this to work" - it seem that information in posts you've linked to should be enough - so not clear what part you trying to clarify. – Alexei Levenkov Oct 21 '15 at 01:39
  • @AlexeiLevenkov I am targeting 4.5 and according to what I have read and what has been posted here "FormattableString is a new type introduced in .NET 4.6". So is my example only exploiting "string interpolation"? If so what example would exploit string interpolation **with** FormattableString? – TheEdge Oct 21 '15 at 02:30
  • @TheEdge `IFormattable s = $"{i}";` as shown in the post you've linked? – Alexei Levenkov Oct 21 '15 at 02:36
  • @AlexeiLevenkov So if IFormattable was added in 4.6 and I am targeting **4.5** why does my code compile / work? – TheEdge Oct 21 '15 at 02:47
  • Because your code does not use `IFormattable`. What makes you think that you *do use* `IFormattable` in your sample code? – Alexei Levenkov Oct 21 '15 at 02:53
  • @AlexeiLevenkov Can you give me a succinct example that does use IFormattable? – TheEdge Oct 21 '15 at 03:00
  • @MarcinJuraszek `FormattableString` was **not** introduced in .Net 4.5. It was introduced in .Net 4.6. – i3arnon Oct 21 '15 at 05:55
  • String interpolation is a *language*, not a *framework* feature. It works as long as you select C# 6 as your language. In fact, this has been asked and answered a lot of tiimes already – Panagiotis Kanavos Oct 21 '15 at 09:17

2 Answers2

14

As mentioned in the comments, string interpolation works in this case as all the new compiler does is convert the expression into an "equivalent string.Format call" at compile time.

From https://msdn.microsoft.com/en-us/magazine/dn879355.aspx

String interpolation is transformed at compile time to invoke an equivalent string.Format call. This leaves in place support for localization as before (though still with traditional format strings) and doesn’t introduce any post compile injection of code via strings.


The FormattableString is a new class allows you to inspect the string interpolation before rendering so you can check the values and protect against injection attacks.

// this does not require .NET 4.6
DateTime now = DateTime.Now;
string s = $"Hour is {now.Hour}";
Console.WriteLine(s);

//Output: Hour is 13

// this requires >= .NET 4.6
FormattableString fs = $"Hour is {now.Hour}";
Console.WriteLine(fs.Format);
Console.WriteLine(fs.GetArgument(0));

//Output: Hour is {0}
//13
codersl
  • 2,222
  • 4
  • 30
  • 33
  • Not sure how this answer is different/better than the [other one](http://stackoverflow.com/a/33249478/477420) (or how it addresses not-yet-identified concern of OP). – Alexei Levenkov Oct 21 '15 at 01:51
  • Added explanation of FormattableString class and examples. – codersl Oct 21 '15 at 02:39
  • @codersl Syntax error now.Hour --> DateTime.Now.Hour – TheEdge Oct 21 '15 at 03:30
  • I just tried this in VS2015 in a new console project and it works fine. I had to target framework .NET 4.6 though as it doesn't work with 4.5.x (which according to MSDN is the minimum required). – codersl Oct 21 '15 at 04:39
  • @codersl Sorry missed your local variable declaration of "now" – TheEdge Oct 22 '15 at 02:06
8

Can someone explain why this is the case as I would not have expected this to work?

This works since you're compiling with the new Roslyn compiler which ships with VS2015, and knows how to parse the string interpolation syntactic sugar (it simply calls the proper overload of string.Format). If you'd try to take advantage of .NET Framework 4.6 classes that work nicely with string interpolation, such as FormattableString or IFormattable, you'd run into a compile time error (unless you add them yourself. See bottom part of the post).

I am guessing I am missing what FormattableString really means.

FormattableString is a new type introduced in .NET 4.6, which allows you to use the new string interpolation feature with a custom IFormatProvider of your choice. Since this can't be done directly on the interpolated string, you can take advantage of FormattableString.ToString(IFormatProvider) which can be passed any custom format.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • @downvoter So enlighten with a reason for the downvote. – Yuval Itzchakov Oct 21 '15 at 01:28
  • It wasn't me, but [`FormattableString`](https://msdn.microsoft.com/en-us/library/system.formattablestring(v=vs.110).aspx) is in .Net 4.5 – DavidG Oct 21 '15 at 01:29
  • 2
    @DavidG not according to the docs: *.NET Framework **Available since 4.6*** – Yuval Itzchakov Oct 21 '15 at 01:30
  • I felt it was relevant to the discussion between you and DavidG. –  Oct 21 '15 at 01:36
  • 1
    Downvote (not mine) likely due to the fact that information you've provided is already covered in post OP linked to... But there is something OP does not understand - not really clear what so. – Alexei Levenkov Oct 21 '15 at 01:37
  • @Alexi But apparently OP doesn't quite understand the accepted answer. Since when is an attempt to answer a legitimate reason for a downvote? – Yuval Itzchakov Oct 21 '15 at 01:39
  • @YuvalItzchakov since beginning of time :) "not useful" - is subjective measure, but I can see how one can consider your post that way. (flagging NAA would be inappropriate indeed) – Alexei Levenkov Oct 21 '15 at 01:49
  • @Alexei I guess. Maybe I'm too naive to believe the fact that it would be nice of people to shed some light on what's wrong with an answer so one can improve it, instead of "drive-by downvoting" it. Bit, to each his own.. – Yuval Itzchakov Oct 21 '15 at 01:51
  • I have 4.5.1 targeted on my Dev box and string interpolation works fine. I release the code and my Jenkins engineer is complaining that the deployment glitches on that box. Is that because Jenkins doesn't use the newer compiler? Should I switch back to composite formatting or can I get Jenkins to upgrade? – Mike K Feb 16 '17 at 01:30