Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 909
Aaron Stainback Avatar asked Oct 18 '15 21:10

Aaron Stainback


People also ask

What is default culture in C#?

NET Framework 4 and previous versions, by default, the culture of all threads is set to the Windows system culture.

How do I change the current culture in C#?

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.

What is culture in VB net?

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.

What is globalization C#?

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.


1 Answers

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}"); 
like image 106
i3arnon Avatar answered Oct 05 '22 12:10

i3arnon