Page 196 - CSharp/C#
P. 196
}
public static string Invariant(FormattableString formattableString)
{
return formattableString?.ToString(CultureInfo.InvariantCulture);
}
}
Then, to produce a correct string for the current culture, just use the expression:
Culture.Current($"interpolated {typeof(string).Name} string.")
Culture.Invariant($"interpolated {typeof(string).Name} string.")
Note: Current and Invariant cannot be created as extension methods because, by default, the
compiler assigns type String to interpolated string expression which causes the following code to
fail to compile:
$"interpolated {typeof(string).Name} string.".Current();
FormattableString class already contains Invariant() method, so the simplest way of switching to
invariant culture is by relying on using static:
using static System.FormattableString;
string invariant = Invariant($"Now = {DateTime.Now}");
string current = $"Now = {DateTime.Now}";
Behind the scenes
Interpolated strings are just a syntactic sugar for String.Format(). The compiler (Roslyn) will turn it
into a String.Format behind the scenes:
var text = $"Hello {name + lastName}";
The above will be converted to something like this:
string text = string.Format("Hello {0}", new object[] {
name + lastName
});
String Interpolation and Linq
It's possible to use interpolated strings in Linq statements to increase readability further.
https://riptutorial.com/ 142

