The existing answers talk about this being a C# 6 feature without a .NET framework component.
This is entirely true of nameof
- but only somewhat true of string interpolation.
String interpolation will use string.Format
in most cases, but if you're using .NET 4.6, it can also convert an interpolated string into a FormattableString
, which is useful if you want invariant formatting:
using System;
using System.Globalization;
using static System.FormattableString;
class Test
{
static void Main()
{
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
double d = 0.5;
string local = $"{d}";
string invariant = Invariant($"{d}");
Console.WriteLine(local); // 0,5
Console.WriteLine(invariant); // 0.5
}
}
Obviously this wouldn't work if $"{d}"
simply invoked a call to string.Format
... instead, in this case it calls string.Format
in the statement assigning to local
, and calls FormattableStringFactory.Create
in the statement assigning to invariant
, and calls FormattableString.Invariant
on the result. If you try to compile this against an earlier version of the framework, FormattableString
won't exist so it won't compile. You can provide your own implementation of FormattableString
and FormattableStringFactory
if you really want to, though, and the compiler will use them appropriately.