C#: Setting CurrentCulture default for all threads in your application

Posted: (EET/GMT+2)

 

By default, each thread in .NET uses the culture inherited from the OS or the environment. If you want to enforce a specific culture for your entire application, you can set the default thread culture in one place.

In .NET 4.5 and later, use the CultureInfo.DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture properties. For instance, like this:

using System.Globalization;
using System.Threading;

class Program
{
    static void Main()
    {
        CultureInfo fi = new CultureInfo("fi-FI");
        CultureInfo.DefaultThreadCurrentCulture = fi;
        CultureInfo.DefaultThreadCurrentUICulture = fi;

        // all new threads will now use Finnish culture by default
        Thread t = new Thread(() =>
        {
            Console.WriteLine(DateTime.Now.ToString("d"));
            Console.WriteLine(1234.56.ToString("N"));
        });
        t.Start();
        t.Join();
    }
}

This is useful when you want consistent date and number formatting regardless of the underlying OS settings. For ASP.NET applications, you can also configure culture in web.config or in middleware, but for desktop or service applications, setting the default thread culture once at startup keeps things predictable.