Tuesday, 7 January 2020

Get list of country without database by using RegionInfo Class

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
  1. public struct DDLStructure
  2. {
  3.     public String Value { get; set; }
  4.     public String Text { get; set; }
  5. }
Now let's write a simple method in our application anywhere to get the list of countries
  1. public List<DDLStructure> GetCountries()
  2. {                        
  3.     var region = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
  4.                     .Select(x => new RegionInfo(x.LCID));
  5.     var countries = (from x in region
  6.                  select new DDLStructure() { Value = x.ThreeLetterISORegionName, Text = x.EnglishName })
  7.                  .Distinct()
  8.                  .OrderBy(x=>x.Text)
  9.                  .ToList<DDLStructure>();      
  10.     return countries;
  11. }
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
CurrencyEnglishNameGets the name, in English, of the currency used in the country/region.
CurrencyNativeNameGets the name of the currency used in the country/region, formatted in the native language of the country/region.
CurrencySymbolGets the currency symbol associated with the country/region.
CurrentRegionGets the `RegionInfo` that represents the country/region used by the current thread.
DisplayNameGets 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.

No comments:

Post a Comment

Baisic Useful Git Commands

  Pushing a fresh repository Create a fresh repository(Any cloud repository). Open terminal (for mac ) and command (windows) and type the be...