Get list of country without database by using RegionInfo Class

CultureInfo Class RegionInfo Class fill countries from regioninfo class get list of countries without database

RegionInfo class can be used to get the detail of any country and it's other country related information like country name, country code, currency, native name and many more. If we want to bind a drop down with the countries of world without creating database, then it will be the good option. There are many other information which can be used but for this article just we are going to see how we can get the list of countries two properties, Country name and Country Code. For it I am going to create a simple structure which contain only two properties, value and text and that is enough for our example. So Create the structure name DDLStructure

public struct DDLStructure
{
    public String Value { get; set; }
    public String Text { get; set; }

}

Now let's write a simple method in our application anywhere to get the list of countries

public List<DDLStructure> GetCountries()
{                        

    var region = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                    .Select(x => new RegionInfo(x.LCID));

    var countries = (from x in region
                 select new DDLStructure() { Value = x.ThreeLetterISORegionName, Text = x.EnglishName })
                 .Distinct()
                 .OrderBy(x=>x.Text)
                 .ToList<DDLStructure>();      

    return countries;
}

There is nothing complicated to explain, simply get the regions and get complete list of countries.

One thing you need to note, we used distinct to remove duplicate items and sort by country name.

Here is the list of properties which we can use

NameDescription
CurrencyEnglishName Gets the name, in English, of the currency used in the country/region.
CurrencyNativeName Gets the name of the currency used in the country/region, formatted in the native language of the country/region.
CurrencySymbol Gets the currency symbol associated with the country/region.
CurrentRegion Gets the `RegionInfo` that represents the country/region used by the current thread.
DisplayName Gets the full name of the country/region in the language of the localized version of .NET Framework.
EnglishName
GeoIdGets a unique identification number for a geographical region, country, city, or location.
IsMetricGets a value indicating whether the country/region uses the metric system for measurements.
ISOCurrencySymbolGets the three-character ISO 4217 currency symbol associated with the country/region.
NameGets the name or ISO 3166 two-letter country/region code for the current RegionInfo object.
NativeName

Gets the name of a country/region formatted in the native language of the country/region.

ThreeLetterISORegionName

Gets the three-letter code defined in ISO 3166 for the country/region.

ThreeLetterWindowsRegionName

Gets the three-letter code assigned by Windows to the country/region represented by this RegionInfo.

TwoLetterISORegionName

Gets the two-letter code defined in ISO 3166 for the country/region.

We can change the properties, our structure to get different properties according to our need.

CultureInfo Class

It represents information about a specific culture including the names of the culture, the writing system, and the calendar used, as well as access to culture-specific objects that provide methods for common operations, such as formatting dates and sorting strings.

The CultureInfo class holds culture-specific information, such as the associated language, sublanguage, country/region, calendar, and cultural conventions. This class also provides access to culture-specific instances of DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo. These objects contain the information required for culture-specific operations, such as casing, formatting dates and numbers, and comparing strings.

RegionInfo Class

In contrast to CultureInfo, RegionInfo does not represent preferences of the user and does not depend on the user's language or culture.

The RegionInfo name is one of the two-letter codes defined in ISO 3166 for country/region. Case is not significant; however, the Name, the TwoLetterISORegionName, and the ThreeLetterISORegionName properties return the appropriate code in uppercase.

Alicia Gonzalez Started career with .Net, and not working on client side technologies like angular, React etc. since last 12 years. But C# is really amazing language and I like to learn everything about C# and want to share whatever I know.
  • CultureInfo
  • RegionInfo
  • c#
By Alicia Gonzalez On 16 Nov, 15  Viewed: 6,503

Other blogs you may like

default value for different data type in C#

In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi   On 27 Mar 2013  Viewed: 2,599

enum in C# and conversion

An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement,... By Hamden   On 27 Mar 2013  Viewed: 4,525

Dynamic vs var in C# with example

When we have var which dynamically holds any type or value so why we need dynamic type, what are the differences and situations where we can use dynamic rather than var, there were some question in my mind so I explored more which I want to share here. There are two things, static type and... By Nathan Armour   On 26 Mar 2013  Viewed: 3,204

Array in C# - single, multiple and jagged array

An array can be initialized in several ways, it can store a single value or series of values, every element must be set of same type, C# supports single-dimensional arrays, multidimensional arrays (also call rectangular array) and array-of-arrays (referred jagged arrays), let’s discuss features of... By Ali Adravi   On 24 Mar 2013  Viewed: 4,336

XmlDocument Vs XDocument and their usage

When there was XmlDocument why we need XDocument, is there any better feature available with XDocument, answer is yes, XDocument has a number of benefits, easy to create, easy to update, easy to search, ability to use LINQ to XML, cleaner object model etc. In this post we will see some examples... By Ali Adravi   On 16 Mar 2013  Viewed: 3,481