How it is possible to define default culture for an application domain in .NET Framework 4.5?

Inside the application domain when threads get created, threads gets default culture from windows initialized from the system default locale as configured in control panel > region and language settings. But with .NET Framework 4.5 it is possible to provide default culture for an application domain using DefaultThreadCurrentCulture and CurrentUICulture properties of CultureInfo class.

Following is example for the same.

public static void Main()
{
Console.OutputEncoding = Encoding.UTF8;
// Change current culture
CultureInfo culture;
if (Thread.CurrentThread.CurrentCulture.Name == "fr-FR")
culture = CultureInfo.CreateSpecificCulture("en-US");
else
culture = CultureInfo.CreateSpecificCulture("fr-FR");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
// Generate and display three random numbers on the current thread.
DisplayRandomNumbers();
Thread.Sleep(1000);
Thread workerThread = new Thread(new ThreadStart(Example.DisplayRandomNumbers));
workerThread.Start();
}
private static void DisplayRandomNumbers()
{
Console.WriteLine();
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture);
Console.WriteLine("Current UI Culture: {0}",
Thread.CurrentThread.CurrentUICulture);
Console.Write("Random Values: ");
Random rand = new Random();
for (int ctr = 0; ctr <= 2; ctr++)
Console.Write(" {0:C2} ", rand.NextDouble());
Console.WriteLine();
}

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*