Country codes list - C#

Tejaswi Yerukalapudi picture Tejaswi Yerukalapudi · Aug 24, 2009 · Viewed 27.1k times · Source

I have a string which I need to verify if it's a Country code. The culture is German. Is there any method that I can call to get a list of Country codes in a German culture without having to type out all the 274 (?) codes myself?

Thanks, Teja.

Answer

Ostemar picture Ostemar · Aug 24, 2009

When you say "country code" I assume you mean the two-letter code as in ISO 3166. Then you can use the RegionInfo constructor to check if your string is a correct code.

string countryCode = "de";
try {
    RegionInfo info = new RegionInfo(countryCode);
}
catch (ArgumentException argEx)
{
    // The code was not a valid country code
}

You could also, as you state in your question, check if it is a valid country code for the german language. Then you just pass in a specific culture name together with the country code.

string language = "de";
string countryCode = "de";
try {
    RegionInfo info = new RegionInfo(string.Format("{0}-{1}", language, countryCode));
}
catch (ArgumentException argEx)
{
    // The code was not a valid country code for the specified language
}