Defining the culture for an application domain

With .NET 4.5, we have in our hands a way of specifying the default culture for all of our application threads in a quick and efficient way.

How to do it...

We will now define the default culture for our application domain as follows:

  1. Create a new Visual Studio project of type Console Application named caCultureAppDomain.
  2. Open the Program.cs file and add the using clause for globalization:
    using System.Globalization;
  3. Next, add the following methods:
    static void DefineAppDomainCulture() {
    String CultureString = "en-US";
    DisplayCulture();
    CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture(CultureString);
    CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture(CultureString);
    DisplayCulture();
    Console.ReadLine();
    }
    
    static void DisplayCulture() {
    Console.WriteLine("App Domain........: {0}", AppDomain.CurrentDomain.Id);
    Console.WriteLine("Default Culture...: {0}", CultureInfo.DefaultThreadCurrentCulture);
    Console.WriteLine("Default UI Culture: {0}", CultureInfo.DefaultThreadCurrentUICulture);
    }
  4. Then add a call to the DefineAppDomainCulture() method.
  5. If we execute it, we will observe that the initial default cultures are null and we specify them to become the default for the App Domain.
How to do it...

How it works...

We used the CultureInfo class to specify the culture and the UI of the application domain and all its threads. This is easily done through the DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture properties.

There's more...

We must be aware that these properties affect only the current application domain, and if it changes we should control them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset