Getting country names from system


Introduction

The  System.Globalization namespace contains classes that define culture-related information, including the language, the country/region, the calendars in use, the format patterns for dates, currency, and numbers, and the sort order for strings.

The following table displays the classes available in System.Globalization namespace.

Class  Description
Calendar Represents time in divisions, such as weeks, months, and years.
CompareInfo Implements a set of methods for culture-sensitive string comparisons.
CultureInfo 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.
DateTimeFormatInfo Defines how DateTime values are formatted and displayed, depending on the culture.
DayLightTime Defines the period of daylight-saving time.
GregorianCalendar Represents the Gregorian calendar.
HebrewCalendar Represents the Hebrew calendar.
HijriCalendar Represents the Hijri calendar.
JapaneseCalendar Represents the Japanese calendar.
JulianCalendar Represents the Julian calendar.
KoreanCalendar Represents the Korean calendar.
NumberFormatInfo Defines how numeric values are formatted and displayed, depending on the culture.
RegionInfo Contains information about the country/region.
SortKey Maps strings to their sort keys.
StringInfo Provides functionality to split a string into text elements and to iterate through those text elements.
TaiwanCalendar Represents the Taiwan Calendar.
TextElementEnumerator Enumerates the text elements of a string.
TextInfo Defines properties and behaviors, such as casing, that are specific to a writing system.
ThaiBuddhistCalendar Represents the Thai Buddhist calendar

Table 1.1 Classes available in System.Globalization

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.

Our Goal

Our goal is to add country names to the DropDownList control in our web page. We have used the following code in page load event of the web page.

We have used the following classes

  1. RegionInfo Class
  2. List from System.Collections
  3. CultureInfo class using GetCulture() method
  4. CultureTypes class suing SpecificCultures property
  5. LCID property of CultureInfo
  6. IEnumerable interface of System.Collections
  7. Lambda Expression technique to findout distinct country names
  8. DisplayName property of RegionInfo class

Steps followed

  1. First of all to use the above classes and interfaces; include the namespaces such as System.Collections and System.Globalization.
  2. Create an object of RegionInfo class where define the format.
  3. Create a list object where the country names can be stored.
  4. Using foreach add country names to the list. Note that we are extracting the country names from the available languages from the system. So country names are added more than once based on the number of languages available in the country.
  5. To find out distinct country names we have used IEnumerable interface where we can use the Distinct() method and store unique country names.
  6. Finally add the country names to the DropDownList from the IEnumerable.

Here is the code for getting country names from system.

using System.Globalization;

using System.Collection;

RegionInfo country = new RegionInfo(new CultureInfo("en-US", false).LCID);

           

 List<string> countryNames = new List<string>();

          

 //To get the Country Names from the CultureInfo installed in windows

 foreach (CultureInfo cul in CultureInfo.GetCultures(CultureTypes.SpecificCultures))

 {

       country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);

       countryNames.Add(country.DisplayName.ToString());

 }

           

 //Assigning all Country names to IEnumerable

 IEnumerable<string> nameAdded = countryNames.OrderBy(names =>names).Distinct();

 if (!IsPostBack)

 {

      foreach (string item in nameAdded)

      {

          ddlCountry.Items.Add(item); //Adding items to DropDownList

      }

 }


Similar Articles