Click here to Skip to main content
14,427,915 members

C# CountryInfo LanguageInfo CurrencyInfo Classes

Rate this:
5.00 (3 votes)
Please Sign up or sign in to vote.
5.00 (3 votes)
20 Jun 2014MIT
C# CountryInfo class, LanguageInfo class and CurrencyInfo class, derived from CultureInfo respectively RegionInfo for a more intuitive and compile-safe handling of countries, languages and currencies


CultureInfo and RegionInfo are great classes, providing a real lot of useful information about languages, countries, currencies and much more. But I found them to be quite complicated, partially hard to use and confusing.

I'm rather thinking in countries, languages and currencies. Maybe someone else has already solved this, but I did not find a suitable solution. So I wrote three classes CountryInfo, LanguageInfo and CurrencyInfo.

They are reasonably linked among each other and offer additional information, not provided by or not easily accessible using the respective .NET classes. For example:

The languages spoken in a country (see languages in Canada):

Image 1

The default language for a country (e.g. English not Spanish for the US):

Image 2

The countries where a language is spoken (see countries for French):

Image 3

The origin of a language:

Image 4

The countries where a currency is used (see countries for US Dollar):

Image 5

Additionally, I wrote some extensions to CultureInfo to get the corresponding CountryInfo, LanguageInfo and CurrencyInfo from a CultureInfo at hand.

Together with the linking between entities mentioned above, you can even expose a default country and currency for a neutral (not country-related) culture:

Image 6

This may not necessarily always make sense, though:

Image 7

But for specific cultures, all is ok:

Image 8

Using the Code

What struck me, too, is that CultureInfo as well as RegionInfo are identified and created by either a string, e.g. "en-US" or an integer (LCID). When you pass a non-existing string or integer to the constructor, you get an exception.

I would have wished to have an enum, e.g. "CultureCode", where each enum value represents a culture. This would be much easier and compile-safer to deal with.

Thus, for my classes, I introduced three enums:


/// <summary>
/// All countries supported by the .Net framework.
/// </summary>
/// <remarks>
/// Enum names are derived from <see cref="RegionInfo.TwoLetterISORegionName"/>, 
/// which is the same as <see cref="RegionInfo.Name"/>.
/// The region "Caribbean" is an exception. It does not represent a single country 
/// and has TwoLetterISORegionName "029", which cannot be converted to an enum. 
/// Here, <see cref="CountryCode.Cb"/> is used as a workaround. 
/// Country code "CB" is officially unassigned (see
/// </remarks>
public enum CountryCode
    /// <summary>
    /// U.A.E.
    /// </summary>

    /// <summary>
    /// Afghanistan
    /// </summary>

    /// <summary>
    /// Albania
    /// </summary>


/// <summary>
/// All (parent) languages supported by the .Net framework.
/// </summary>
/// <remarks>
/// Names are derived from <see cref="CultureInfo.TwoLetterISOLanguageName"/>, 
/// which is the same as <see cref="CultureInfo.Name"/>.
/// For simplification, neither the way how they are written (e.g. latin or cyrillic) 
/// nor regional dialects (e.g. northern sami, southern sami) are taken into account here. 
/// Sub-cultures / sub-languages are mapped to their parents. 
/// </remarks>
public enum LanguageCode
    /// <summary>
    /// Afrikaans
    /// </summary>

    /// <summary>
    /// Amharic
    /// </summary>

    /// <summary>
    /// Arabic
    /// </summary>

    /// <summary>
    /// Mapudungun
    /// </summary>


/// <summary>
/// All currencies supported by the .Net framework.
/// </summary>
/// <remarks>
/// Names are derived from <see cref="RegionInfo.ISOCurrencySymbol"/>.
/// </remarks>
public enum CurrencyCode
    /// <summary>
    /// UAE Dirham
    /// </summary>

    /// <summary>
    /// Afghani
    /// </summary>

    /// <summary>
    /// Albanian Lek
    /// </summary>

    /// <summary>
    /// Armenian Dram
    /// </summary>

The respective CountryInfo, LanguageInfo and CurrencyInfo instances can easily be got from a static method of the respective class:

CountryInfo countryInfo = CountryInfo.GetCountry(CountryCode.Us);
LanguageInfo languageInfo = LanguageInfo.GetLanguage(LanguageCode.Es);
CurrencyInfo currencyInfo = CurrencyInfo.GetCurrency(CurrencyCode.Eur);

Additionally, the enum approach provides nice intellisense!

All CountryInfo, LanguageInfo and CurrencyInfo instances are created in static constructors of the respective classes and are kept in an internal dictionary. It can be accessed over the static All property, each of the classes provides, for example:

/// <summary>
/// A sorted dictionary with all supported <see cref="CountryInfo"/>s.
/// </summary>
/// <remarks>
/// Returns a copy of the internal dictionary. For performance reasons, cache this locally.
/// Also for performance reasons, do not use this property to get a 
/// specific <see cref="CountryInfo"/>. Use <see cref="GetCountry(CountryCode)"/> instead.
/// </remarks>
public static SortedDictionary<CountryCode, CountryInfo> All
        return new SortedDictionary<CountryCode, CountryInfo>(_all);

The instance constructors are made private, so you always get the same, e.g., CountryInfo instance when calling CountryInfo.GetCountry(CountryCode):

/// <summary>
/// Gets a <see cref="CountryInfo"/> by its <paramref name="countryCode"/>.
/// </summary>
/// <remarks>
/// Never returns null.
/// </remarks>
/// <param name="countryCode">
/// The <see cref="CountryCode"/> to get the <see cref="CountryInfo"/> for.
/// </param>
/// <returns>
/// The <see cref="CountryInfo"/> corresponding to <paramref name="countryCode"/>.
/// </returns>
public static CountryInfo GetCountry(CountryCode countryCode)
    return _all[countryCode];

As all collections (e.g. all countries where a language is spoken) in instances are always returned as a copy, the whole system is immutable.

Of course, particular attention has been given to thread-safety, therefore it should be guaranteed throughout. In the demo project, where the above screenshots are taken from, some unit tests are included, which should cover all functionality provided. Thus, I expect the classes to be very robust.

Points of Interest

There were some edge-cases which made it partially hard to maintain consistency. For example, region "Caribbean", which does not represent a single country, but (really) a region. It has TwoLetterISORegionName "029", which cannot be converted to an enum. Also the invariant culture was an issue. Explore the source code how I solved both cases.

Some extra information my classes provide are subject to research and/or a matter of taste. For example, I've added a property ShortEnglishName for countries to say e.g. just Monaco instead of Principality of Monaco as it comes from MS. Same applies for default languages and default countries. In some cases, I may have been incorrect. As you have the source code, feel free to change it, if you do not agree.


  • 19 June 2014 - First published, version 1.0
  • 23 June 2014 - Added VehicleRegistrationCode and GermanName to CountryInfo


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Jochen Scharr
Software Developer (Senior)
Germany Germany
No Biography provided

Comments and Discussions

QuestionGood article Pin
rammanusani26-Mar-19 9:08
Memberrammanusani26-Mar-19 9:08 
QuestionCountryInfo in Demo app error Pin
Edgars Palacis24-Feb-15 2:49
MemberEdgars Palacis24-Feb-15 2:49 
AnswerRe: CountryInfo in Demo app error Pin
Jochen Scharr25-Feb-15 5:50
MemberJochen Scharr25-Feb-15 5:50 
GeneralRe: CountryInfo in Demo app error Pin
Edgars Palacis28-Feb-15 2:58
MemberEdgars Palacis28-Feb-15 2:58 
QuestionGithub repo Pin
rtimon28-Nov-14 4:54
Memberrtimon28-Nov-14 4:54 
AnswerRe: Github repo Pin
Jochen Scharr28-Nov-14 7:55
MemberJochen Scharr28-Nov-14 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 20 Jun 2014


10 bookmarked