What is the default culture for C# 6 string interpolation?

C#String InterpolationC# 6.0

C# Problem Overview


In C# 6 what is the default culture for the new string interpolation?

I've seen conflicting reports of both Invariant and Current Culture.

I would like a definitive answer and I'm keeping my fingers crossed for Invariant.

C# Solutions


Solution 1 - C#

Using string interpolation in C# is compiled into a simple call to String.Format. You can see with TryRolsyn that this:

public void M()
{
    string name = "bar";
    string result = $"{name}";
}

Is compiled into this:

public void M()
{
    string arg = "bar";
    string text = string.Format("{0}", arg);
}

It's clear that this doesn't use an overload that accepts a format provider, hence it uses the current culture.

You can however compile the interpolation into FormattbleString instead which keeps the format and arguments separate and pass a specific culture when generating the final string:

FormattableString formattableString = $"{name}";
string result = formattableString.ToString(CultureInfo.InvariantCulture);

Now since (as you prefer) it's very common to use InvariantCulture specifically there's a shorthand for that:

string result = FormattableString.Invariant($"{name}");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAaron StainbackView Question on Stackoverflow
Solution 1 - C#i3arnonView Answer on Stackoverflow