The community is working on translating this tutorial into Telugu, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
The RegionInfo class
In the previous article on the CultureInfo class, we discussed the country/region part of it a bit, but we can actually do way more region-based stuff with one of the other classes in the System.Globalization namespace: The RegionInfo class. It will contain a lot of useful information about a specific region (usually a country), e.g. the name and symbol of their currency, whether they use the metric system or not and so on.
Obtaining a RegionInfo instance
To get access to regional info, you need an instance of the RegionInfo class. It has a constructor which can take an ISO 3166 code or the languagecode/region-country code of the region (e.g. "en-US"). Here's an example:
RegionInfo regionInfo = new RegionInfo("en-US");
Console.WriteLine(regionInfo.EnglishName);
This also means that if you already have a reference to a CultureInfo class, you can easily use this to make sure that you get the matching RegionInfo. And as we learned in a previous article, your application always has a fallback CultureInfo instance that you can reference:
RegionInfo regionInfo = new RegionInfo(CultureInfo.CurrentCulture.Name);
Console.WriteLine(regionInfo.EnglishName);
With that in place, let's have a look at some of the useful features of the RegionInfo class.
Important properties of the RegionInfo class
We already checked out the EnglishName property - it simply returns the name of the region, in English. But of course there's more good stuff in there - for instance several properties related to currency:
RegionInfo regionInfo = new RegionInfo("sv-SE");
Console.WriteLine(regionInfo.CurrencySymbol);
Console.WriteLine(regionInfo.ISOCurrencySymbol);
Console.WriteLine(regionInfo.CurrencyEnglishName);
Console.WriteLine(regionInfo.CurrencyNativeName);
Using CurrencySymbol, ISOCurrencySymbol, CurrencyEnglishName and/or CurrencyNativeName, we get the information we need to output monetarily related messages. The result will look like this (in this case for Swedish/Sweden):
kr
SEK
Swedish Krona
Svensk krona
You can also easily check if the given region uses the metric system, using the IsMetric property:
RegionInfo regionInfo = new RegionInfo(CultureInfo.CurrentCulture.Name);
Console.WriteLine("Is the metric system used in " + regionInfo.EnglishName + "? " + (regionInfo.IsMetric ? "Yes" : "No"));
That leaves us with all the identity related properties:
- Name will get you the ISO 3166 code which identifies the language and country/region, e.g. "en-US"_ for English/United States.
- DisplayName will get you the full name of the country/region in the localized .NET framework version.
- EnglishName will get you the full name of the country/region in English.
- NativeName will get you the full name of the country/region in the given language, e.g. "United States" for en-US or "Deutschland" for de-DE.
- TwoLetterISORegionName will get you the two-letter ISO 3166 code for the country/region, e.g. "US" for the United States or "DE" for Germany.
- ThreeLetterISORegionName will get you the three-letter ISO 3166 code for the country/region, e.g. "USA" for the United States or "DEU" for Germany.
Obviously, these properties will come in handy when you have to display information about a country/region, as we'll see in our next example.
Getting a list of countries with RegionInfo
In a previous article, I showed you how to get a list of all the defined cultures in the .NET framework, which basically leaves us with a list of language-country/region combinations. We can use that in combination with the RegionInfo class to get a list of countries/regions:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace RegionInfoCountries
{
class Program
{
static void Main(string[] args)
{
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
List<RegionInfo> countries = new List<RegionInfo>();
foreach (CultureInfo ci in cultures)
{
RegionInfo regionInfo = new RegionInfo(ci.Name);
if (countries.Count(x => x.EnglishName == regionInfo.EnglishName) <= 0)
countries.Add(regionInfo);
}
foreach (RegionInfo regionInfo in countries.OrderBy(x => x.EnglishName))
Console.WriteLine(regionInfo.EnglishName);
}
}
}
So, this example is a bit longer than the other examples in this article, but let me break it down for you. We start by getting a list of all available, specific cultures - as we learned in a previous article, specific cultures are the ones who define a language AND a region/country. We loop through this list and on each iteration, we use the CultureInfo instance to create a corresponding RegionInfo instance. We check if a country with that name has already been added to our list and if it hasn't, we add it. When the loop has finished, we now have a complete list of the countries defined by the .NET framework, which we can loop through and output to the console, or do pretty much anything else that we might find useful.
This is a great example of what you can accomplish with the combination of CultureInfo and RegionInfo, but allow me to emphasize that this is not a complete and accurate list of countries. It is instead a complete list of countries defined in the version of the .NET framework you're using, basically meaning that some countries could be missing or may have changed their name since the version was released. So, if you need a list of countries which is 100% accurate and up-to-date, you will probably have to create and maintain it yourself.
Summary
The RegionInfo class is basically an extension of the CultureInfo class with even more useful information about a specific country/region. It allows you to know more about the identity and currency of a specific region, and it can help you get a list of countries/regions.