how to set default culture info for entire c# application

C#C# 4.0GlobalizationCultureinfoInvariantculture

C# Problem Overview


I want to set default culture info for that class or for entire application.

For example in Turkey 3,2 = in english 3.2

so application uses my local but i want it to use as default

System.Globalization.CultureInfo.InvariantCulture

How can i set it to that as default for that specific class or for entire application

C# Solutions


Solution 1 - C#

Not for entire application or particular class.

CurrentUICulture and CurrentCulture are settable per thread as discussed here https://stackoverflow.com/questions/468791/setting-currentculture-and-currentuiculture-of-an-application. You can't change InvariantCulture at all.

Sample code to change cultures for current thread:

CultureInfo ci = new CultureInfo(theCultureString);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;

For class you can set/restore culture inside critical methods, but it would be significantly safe to use appropriate overrides for most formatting related methods that take culture as one of arguments:

(3.3).ToString(new CultureInfo("fr-FR"))

Solution 2 - C#

With 4.0, you will need to manage this yourself by setting the culture for each thread as Alexei describes. But with 4.5, you can define a culture for the appdomain and that is the preferred way to handle this. The relevant apis are CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture.

Solution 3 - C#

If you use a Language Resource file to set the labels in your application you need to set the its value:

CultureInfo customCulture = new CultureInfo("en-US");
Languages.Culture = customCulture;

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
QuestionMonsterMMORPGView Question on Stackoverflow
Solution 1 - C#Alexei LevenkovView Answer on Stackoverflow
Solution 2 - C#Eric MSFTView Answer on Stackoverflow
Solution 3 - C#Renzo CiotView Answer on Stackoverflow