views:

56

answers:

2

I am using this standard code for populating list of countries:

static void Main(string[] args)
{
    List cultureList = new List();

    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

    foreach (CultureInfo culture in cultures)
    {
        try
        {
            RegionInfo region = new RegionInfo(culture.LCID);

            if (!(cultureList.Contains(region.EnglishName)))
            {
                cultureList.Add(region.EnglishName);
                Console.WriteLine(region.EnglishName);
            }
        }
        catch (ArgumentException ex) 
        {
            // just ignor this
            continue;
        }
    }
}

I saw that some countries are missed. Just wondered what's the reason of such situation?

+2  A: 

You are not getting all cultures:

CultureTypes.AllCultures & ~CultureTypes.NeutralCultures
Oded
Oded, in the context of the question you're wrong. Neutral cultures don't represent a country, but only a language, and they are very limited in their use (mostly useful only for localizing resources or to get a specific culture from them).
Lucero
@Lucero - he is doing a bitwise complement on `NeutralCultures`.
Oded
@Oded, which is correct because he does *not* want the neutral cultures in the result: `Console.WriteLine(CultureTypes.AllCultures ` returns `SpecificCultures, InstalledWin32Cultures` which should include all specific cultures (e.g. those with countries).
Lucero
A: 

I would use CultureTypes.SpecificCultures but it does not answer your question.

Why there is only subset of world's countries? Well, there are so many of them. Somebody would have to maintain them and it does cost money. I think that's why Microsoft decided to support only the most "popular" ones.

BTW. You may create your own CultureInfo. Also, I haven't tried, but you can create RegionInfo instance by passing its ISO code in constructor. I am not sure what will happen if there is no matching CultureInfo, though.

Paweł Dyda