Difference between CultureInfo.CreateSpecificCulture() and the constructor of the class?

Marcel picture Marcel · Aug 30, 2012 · Viewed 15k times · Source

The class CultureInfo provides two way of creation:

The MSDN documentation does slightly differ for the two, mentioning some "Windows culture" for the constructor. But does that really matter?

Should I prefer one of the two over the other?

Note: I am using .NET version 3.5 if that matters, and I want to use it like this:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

as described in this answer.

Answer

Erwin picture Erwin · Aug 30, 2012

The factory method has an fallback when it fails to create the culture info.

So if you use a specific culture like 'en-XX', the culture info instance can't be created, an exception will throw and a retry with the neutral culture 'en' will succeed.

Below the source of the factory method

public static CultureInfo CreateSpecificCulture(string name)
{
    CultureInfo info;
    try
    {
        info = new CultureInfo(name);
    }
    catch (ArgumentException)
    {
        info = null;
        for (int i = 0; i < name.Length; i++)
        {
            if ('-' == name[i])
            {
                try
                {
                    info = new CultureInfo(name.Substring(0, i));
                    break;
                }
                catch (ArgumentException)
                {
                    throw;
                }
            }
        }
        if (info == null)
        {
            throw;
        }
    }
    if (!info.IsNeutralCulture)
    {
        return info;
    }
    return new CultureInfo(info.m_cultureData.SSPECIFICCULTURE);
}

So the I prefer the factory method.