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
Name | Description |
---|---|
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 | |
GeoId | Gets a unique identification number for a geographical region, country, city, or location. |
IsMetric | Gets a value indicating whether the country/region uses the metric system for measurements. |
ISOCurrencySymbol | Gets the three-character ISO 4217 currency symbol associated with the country/region. |
Name | Gets 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