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.
NET Framework 4 and previous versions, by default, the culture of all threads is set to the Windows system culture.
CurrentCulture = New CultureInfo("th-TH", False) Console. WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name) ' Display the name of the current UI culture. Console. WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name) ' Change the current UI culture to ja-JP.
The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.
Globalization is the process of designing and developing applications that function for multiple cultures. Localization is the process of customizing your application for a given culture and locale.
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}");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With