Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Country codes list - C#

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.

like image 212
Tejaswi Yerukalapudi Avatar asked Aug 24 '09 18:08

Tejaswi Yerukalapudi


People also ask

What is country ID?

ID is the two-letter country abbreviation for Indonesia.

What country code is 011?

The following is a list of countries classified by the international call prefix that needs to be dialled when calling abroad from those countries. 011 – all countries and territories in the North American Numbering Plan: American Samoa. Anguilla.


1 Answers

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
}
like image 83
Ostemar Avatar answered Oct 12 '22 02:10

Ostemar