Quantcast
You are here: Home » Web Development » ASP.NET » Get List of All Countries in C#.NET
Get List of All Countries in C#.NET

Get List of All Countries in C#.NET

If you are developing a software in .NET framework and wants to get list of all countries to fill a Combo Box or any other control, or you wan to use it for any other purpose, then you can use the System.Globalization namespace to generate a list of countries.

System.Globalization namespace contains culture related information which includes (Language, Country/Region, Calendars, dates format and patterns, currency, and numbers). These classes help developers to in writing globalized (international) applications. CultureInfo class provides specific culture information. This information includes name of the culture, calendar used, formatting dates, sort strings, and writing systems.

Countries name is available in RegionInfo class a child class of CultureInfo class and we can get list of all countries from RegionInfo class, for this I wrote the function below which returns list of all countries in array format:

public static List<string> GetCountryList()
{
    List<string> _CultureList = new List<string>();

    CultureInfo[] _CultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

    foreach (CultureInfo _Culture in _CultureInfo)
    {
        RegionInfo _RegionInfo = new RegionInfo(_Culture.LCID);

        if (!(_CultureList.Contains(_RegionInfo.EnglishName)))
        {
            _CultureList.Add(_RegionInfo.EnglishName);
        }
    }
    _CultureList.Sort();
    return _CultureList;
}

The above function will return list of all countries in List<T> array format and then it can be used for any control to fill its DataSource.

Example:

ddlCountry.DataSource = GetCountryList();
ddlCountry.DataBind();

 

We can use List<T>.Sort() function to sort the array.

Remember to add using System.Globalization reference at the top of the page.

 

About Noor Ahmad Feroozi

Over eight years of IT industry experience, and have been a part of design and development projects for many exceptional companies... Read my full profile

Comments are closed.

Scroll To Top
%d bloggers like this: