Skip to content

Instantly share code, notes, and snippets.

@Venoli
Last active October 31, 2024 09:33
Show Gist options
  • Save Venoli/685c5bb24ba8170a7b22f46089d77224 to your computer and use it in GitHub Desktop.
Save Venoli/685c5bb24ba8170a7b22f46089d77224 to your computer and use it in GitHub Desktop.
List of Country Codes and Country Names - C#
using System;
using System.Globalization;
using PhoneNumbers;
using System.Collections.Generic;
using System.Threading.Tasks;
public class CountryCodes
{
public static void Main()
{
getCountryCodes();
}
public static async void getCountryCodes(){
CultureInfo[] cinfo = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
Dictionary<string,int> countryNameAndCode = new Dictionary<string,int>();
 
foreach (CultureInfo cul in cinfo)
{
char [] name = cul.Name.ToCharArray();
if(name.Length >=2){
string twoLetterCode = ""+name[name.Length-2]+name[name.Length-1];
int code = findCountryCode(twoLetterCode);
//Console.WriteLine("Display Name: "+cul.DisplayName + " Name: " + cul.Name +" twoLetterCode: "+ twoLetterCode
//+" Country code: "+code);
try{
var rigion = new RegionInfo(cul.Name);
countryNameAndCode[rigion.EnglishName]=code;
}catch(ArgumentException e){
Console.WriteLine(e.ToString());
}
}
     }
 
    
await Task.WhenAll();
Console.WriteLine("Number of Countries: "+countryNameAndCode.Count);
foreach(var kvp in countryNameAndCode)
Console.WriteLine("Country name: {0}, Country code: {1}", kvp.Key, kvp.Value);
printDictionaryOutput(countryNameAndCode);
}
public static int findCountryCode(string countryShortCode) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
return phoneUtil.GetCountryCodeForRegion(countryShortCode.ToUpper());
}
public static void printDictionaryOutput(Dictionary<string,int> countryNameAndCode){
foreach(var kvp in countryNameAndCode)
Console.WriteLine("Country name: {0}, Country code: {1},", kvp.Key, kvp.Value);
Console.WriteLine("var countryCodesMapping = new Dictionary<string, string>() {");
foreach (var mapping in countryNameAndCode.OrderBy(mapping => mapping.Key))
{
Console.WriteLine(" {{ \"{0}\", \"{1}\" }},", mapping.Key, mapping.Value);
}
Console.WriteLine("};");
}
}
var countryCodesMapping = new Dictionary<string, string>() {
{ "Afghanistan", "93" },
{ "Åland Islands", "358" },
{ "Albania", "355" },
{ "Algeria", "213" },
{ "American Samoa", "1" },
{ "Andorra", "376" },
{ "Angola", "244" },
{ "Anguilla", "1" },
{ "Antigua and Barbuda", "1" },
{ "Argentina", "54" },
{ "Armenia", "374" },
{ "Aruba", "297" },
{ "Australia", "61" },
{ "Austria", "43" },
{ "Azerbaijan", "994" },
{ "Bahamas", "1" },
{ "Bahrain", "973" },
{ "Bangladesh", "880" },
{ "Barbados", "1" },
{ "Belarus", "375" },
{ "Belgium", "32" },
{ "Belize", "501" },
{ "Benin", "229" },
{ "Bermuda", "1" },
{ "Bhutan", "975" },
{ "Bolivia", "591" },
{ "Bonaire, Sint Eustatius and Saba", "599" },
{ "Bosnia and Herzegovina", "387" },
{ "Botswana", "267" },
{ "Brazil", "55" },
{ "British Indian Ocean Territory", "246" },
{ "British Virgin Islands", "1" },
{ "Brunei", "673" },
{ "Bulgaria", "359" },
{ "Burkina Faso", "226" },
{ "Burundi", "257" },
{ "Cabo Verde", "238" },
{ "Cambodia", "855" },
{ "Cameroon", "237" },
{ "Canada", "1" },
{ "Caribbean", "0" },
{ "Cayman Islands", "1" },
{ "Central African Republic", "236" },
{ "Chad", "235" },
{ "Chile", "56" },
{ "China", "86" },
{ "Christmas Island", "61" },
{ "Cocos (Keeling) Islands", "61" },
{ "Colombia", "57" },
{ "Comoros", "269" },
{ "Congo", "242" },
{ "Congo (DRC)", "243" },
{ "Cook Islands", "682" },
{ "Costa Rica", "506" },
{ "Côte d’Ivoire", "225" },
{ "Croatia", "385" },
{ "Cuba", "53" },
{ "Curaçao", "599" },
{ "Cyprus", "357" },
{ "Czechia", "420" },
{ "Denmark", "45" },
{ "Djibouti", "253" },
{ "Dominica", "1" },
{ "Dominican Republic", "1" },
{ "Ecuador", "593" },
{ "Egypt", "20" },
{ "El Salvador", "503" },
{ "Equatorial Guinea", "240" },
{ "Eritrea", "291" },
{ "Estonia", "372" },
{ "Ethiopia", "251" },
{ "Europe", "0" },
{ "Falkland Islands", "500" },
{ "Faroe Islands", "298" },
{ "Fiji", "679" },
{ "Finland", "358" },
{ "France", "33" },
{ "French Guiana", "594" },
{ "French Polynesia", "689" },
{ "Gabon", "241" },
{ "Gambia", "220" },
{ "Georgia", "995" },
{ "Germany", "49" },
{ "Ghana", "233" },
{ "Gibraltar", "350" },
{ "Greece", "30" },
{ "Greenland", "299" },
{ "Grenada", "1" },
{ "Guadeloupe", "590" },
{ "Guam", "1" },
{ "Guatemala", "502" },
{ "Guernsey", "44" },
{ "Guinea", "224" },
{ "Guinea-Bissau", "245" },
{ "Guyana", "592" },
{ "Haiti", "509" },
{ "Honduras", "504" },
{ "Hong Kong SAR", "852" },
{ "Hungary", "36" },
{ "Iceland", "354" },
{ "India", "91" },
{ "Indonesia", "62" },
{ "Iran", "98" },
{ "Iraq", "964" },
{ "Ireland", "353" },
{ "Isle of Man", "44" },
{ "Israel", "972" },
{ "Italy", "39" },
{ "Jamaica", "1" },
{ "Japan", "81" },
{ "Jersey", "44" },
{ "Jordan", "962" },
{ "Kazakhstan", "7" },
{ "Kenya", "254" },
{ "Kiribati", "686" },
{ "Korea", "82" },
{ "Kosovo", "383" },
{ "Kuwait", "965" },
{ "Kyrgyzstan", "996" },
{ "Laos", "856" },
{ "Latin America", "0" },
{ "Latvia", "371" },
{ "Lebanon", "961" },
{ "Lesotho", "266" },
{ "Liberia", "231" },
{ "Libya", "218" },
{ "Liechtenstein", "423" },
{ "Lithuania", "370" },
{ "Luxembourg", "352" },
{ "Macao SAR", "853" },
{ "Macedonia, FYRO", "389" },
{ "Madagascar", "261" },
{ "Malawi", "265" },
{ "Malaysia", "60" },
{ "Maldives", "960" },
{ "Mali", "223" },
{ "Malta", "356" },
{ "Marshall Islands", "692" },
{ "Martinique", "596" },
{ "Mauritania", "222" },
{ "Mauritius", "230" },
{ "Mayotte", "262" },
{ "Mexico", "52" },
{ "Micronesia", "691" },
{ "Moldova", "373" },
{ "Monaco", "377" },
{ "Mongolia", "976" },
{ "Montenegro", "382" },
{ "Montserrat", "1" },
{ "Morocco", "212" },
{ "Mozambique", "258" },
{ "Myanmar", "95" },
{ "Namibia", "264" },
{ "Nauru", "674" },
{ "Nepal", "977" },
{ "Netherlands", "31" },
{ "New Caledonia", "687" },
{ "New Zealand", "64" },
{ "Nicaragua", "505" },
{ "Niger", "227" },
{ "Nigeria", "234" },
{ "Niue", "683" },
{ "Norfolk Island", "672" },
{ "North Korea", "850" },
{ "Northern Mariana Islands", "1" },
{ "Norway", "47" },
{ "Oman", "968" },
{ "Pakistan", "92" },
{ "Palau", "680" },
{ "Palestinian Authority", "970" },
{ "Panama", "507" },
{ "Papua New Guinea", "675" },
{ "Paraguay", "595" },
{ "Peru", "51" },
{ "Philippines", "63" },
{ "Pitcairn Islands", "0" },
{ "Poland", "48" },
{ "Portugal", "351" },
{ "Puerto Rico", "1" },
{ "Qatar", "974" },
{ "Réunion", "262" },
{ "Romania", "40" },
{ "Russia", "7" },
{ "Rwanda", "250" },
{ "Saint Barthélemy", "590" },
{ "Saint Kitts and Nevis", "1" },
{ "Saint Lucia", "1" },
{ "Saint Martin", "590" },
{ "Saint Pierre and Miquelon", "508" },
{ "Saint Vincent and the Grenadines", "1" },
{ "Samoa", "685" },
{ "San Marino", "378" },
{ "São Tomé and Príncipe", "239" },
{ "Saudi Arabia", "966" },
{ "Senegal", "221" },
{ "Serbia", "381" },
{ "Seychelles", "248" },
{ "Sierra Leone", "232" },
{ "Singapore", "65" },
{ "Sint Maarten", "1" },
{ "Slovakia", "421" },
{ "Slovenia", "386" },
{ "Solomon Islands", "677" },
{ "Somalia", "252" },
{ "South Africa", "27" },
{ "South Sudan", "211" },
{ "Spain", "34" },
{ "Sri Lanka", "94" },
{ "St Helena, Ascension, Tristan da Cunha", "290" },
{ "Sudan", "249" },
{ "Suriname", "597" },
{ "Svalbard and Jan Mayen", "47" },
{ "Swaziland", "268" },
{ "Sweden", "46" },
{ "Switzerland", "41" },
{ "Syria", "963" },
{ "Taiwan", "886" },
{ "Tajikistan", "992" },
{ "Tanzania", "255" },
{ "Thailand", "66" },
{ "Timor-Leste", "670" },
{ "Togo", "228" },
{ "Tokelau", "690" },
{ "Tonga", "676" },
{ "Trinidad and Tobago", "1" },
{ "Tunisia", "216" },
{ "Turkey", "90" },
{ "Turkmenistan", "993" },
{ "Turks and Caicos Islands", "1" },
{ "Tuvalu", "688" },
{ "U.S. Outlying Islands", "0" },
{ "U.S. Virgin Islands", "1" },
{ "Uganda", "256" },
{ "Ukraine", "380" },
{ "United Arab Emirates", "971" },
{ "United Kingdom", "44" },
{ "United States", "1" },
{ "Uruguay", "598" },
{ "Uzbekistan", "998" },
{ "Vanuatu", "678" },
{ "Vatican City", "39" },
{ "Venezuela", "58" },
{ "Vietnam", "84" },
{ "Wallis and Futuna", "681" },
{ "World", "0" },
{ "Yemen", "967" },
{ "Zambia", "260" },
{ "Zimbabwe", "263" },
};
Number of Countries: 248
Country name: Djibouti, Country code: 253
Country name: Eritrea, Country code: 291
Country name: Ethiopia, Country code: 251
Country name: Afghanistan, Country code: 93
Country name: Namibia, Country code: 264
Country name: South Africa, Country code: 27
Country name: Cameroon, Country code: 237
Country name: Ghana, Country code: 233
Country name: Armenia, Country code: 374
Country name: Argentina, Country code: 54
Country name: World, Country code: 0
Country name: United Arab Emirates, Country code: 971
Country name: Bahrain, Country code: 973
Country name: Algeria, Country code: 213
Country name: Egypt, Country code: 20
Country name: Israel, Country code: 972
Country name: Iraq, Country code: 964
Country name: Jordan, Country code: 962
Country name: Comoros, Country code: 269
Country name: Kuwait, Country code: 965
Country name: Lebanon, Country code: 961
Country name: Libya, Country code: 218
Country name: Morocco, Country code: 212
Country name: Mauritania, Country code: 222
Country name: Oman, Country code: 968
Country name: Palestinian Authority, Country code: 970
Country name: Qatar, Country code: 974
Country name: Saudi Arabia, Country code: 966
Country name: Sudan, Country code: 249
Country name: Somalia, Country code: 252
Country name: South Sudan, Country code: 211
Country name: Syria, Country code: 963
Country name: Chad, Country code: 235
Country name: Tunisia, Country code: 216
Country name: Yemen, Country code: 967
Country name: Chile, Country code: 56
Country name: American Samoa, Country code: 1
Country name: India, Country code: 91
Country name: Tanzania, Country code: 255
Country name: Spain, Country code: 34
Country name: Azerbaijan, Country code: 994
Country name: Bosnia and Herzegovina, Country code: 387
Country name: Russia, Country code: 7
Country name: Belgium, Country code: 32
Country name: Belarus, Country code: 375
Country name: Zambia, Country code: 260
Country name: Bulgaria, Country code: 359
Country name: Nigeria, Country code: 234
Country name: Mali, Country code: 223
Country name: Brunei, Country code: 673
Country name: Bangladesh, Country code: 880
Country name: Bolivia, Country code: 591
Country name: China, Country code: 86
Country name: Brazil, Country code: 55
Country name: France, Country code: 33
Country name: Bahamas, Country code: 1
Country name: Canada, Country code: 1
Country name: Andorra, Country code: 376
Country name: Italy, Country code: 39
Country name: Uganda, Country code: 256
Country name: United States, Country code: 1
Country name: Colombia, Country code: 57
Country name: Czechia, Country code: 420
Country name: Cyprus, Country code: 357
Country name: United Kingdom, Country code: 44
Country name: Denmark, Country code: 45
Country name: Greenland, Country code: 299
Country name: Kenya, Country code: 254
Country name: Germany, Country code: 49
Country name: Austria, Country code: 43
Country name: Switzerland, Country code: 41
Country name: Liechtenstein, Country code: 423
Country name: Luxembourg, Country code: 352
Country name: Niger, Country code: 227
Country name: Maldives, Country code: 960
Country name: Senegal, Country code: 221
Country name: Bhutan, Country code: 975
Country name: Togo, Country code: 228
Country name: Greece, Country code: 30
Country name: Caribbean, Country code: 0
Country name: Europe, Country code: 0
Country name: Antigua and Barbuda, Country code: 1
Country name: Anguilla, Country code: 1
Country name: Australia, Country code: 61
Country name: Barbados, Country code: 1
Country name: Burundi, Country code: 257
Country name: Bermuda, Country code: 1
Country name: Botswana, Country code: 267
Country name: Belize, Country code: 501
Country name: Cocos (Keeling) Islands, Country code: 61
Country name: Cook Islands, Country code: 682
Country name: Christmas Island, Country code: 61
Country name: Dominica, Country code: 1
Country name: Finland, Country code: 358
Country name: Fiji, Country code: 679
Country name: Falkland Islands, Country code: 500
Country name: Micronesia, Country code: 691
Country name: Grenada, Country code: 1
Country name: Guernsey, Country code: 44
Country name: Gibraltar, Country code: 350
Country name: Gambia, Country code: 220
Country name: Guam, Country code: 1
Country name: Guyana, Country code: 592
Country name: Hong Kong SAR, Country code: 852
Country name: Indonesia, Country code: 62
Country name: Ireland, Country code: 353
Country name: Isle of Man, Country code: 44
Country name: British Indian Ocean Territory, Country code: 246
Country name: Jersey, Country code: 44
Country name: Jamaica, Country code: 1
Country name: Kiribati, Country code: 686
Country name: Saint Kitts and Nevis, Country code: 1
Country name: Cayman Islands, Country code: 1
Country name: Saint Lucia, Country code: 1
Country name: Liberia, Country code: 231
Country name: Lesotho, Country code: 266
Country name: Madagascar, Country code: 261
Country name: Marshall Islands, Country code: 692
Country name: Macao SAR, Country code: 853
Country name: Northern Mariana Islands, Country code: 1
Country name: Montserrat, Country code: 1
Country name: Malta, Country code: 356
Country name: Mauritius, Country code: 230
Country name: Malawi, Country code: 265
Country name: Malaysia, Country code: 60
Country name: Norfolk Island, Country code: 672
Country name: Netherlands, Country code: 31
Country name: Nauru, Country code: 674
Country name: Niue, Country code: 683
Country name: New Zealand, Country code: 64
Country name: Papua New Guinea, Country code: 675
Country name: Philippines, Country code: 63
Country name: Pakistan, Country code: 92
Country name: Pitcairn Islands, Country code: 0
Country name: Puerto Rico, Country code: 1
Country name: Palau, Country code: 680
Country name: Rwanda, Country code: 250
Country name: Solomon Islands, Country code: 677
Country name: Seychelles, Country code: 248
Country name: Sweden, Country code: 46
Country name: Singapore, Country code: 65
Country name: St Helena, Ascension, Tristan da Cunha, Country code: 290
Country name: Slovenia, Country code: 386
Country name: Sierra Leone, Country code: 232
Country name: Sint Maarten, Country code: 1
Country name: Swaziland, Country code: 268
Country name: Turks and Caicos Islands, Country code: 1
Country name: Tokelau, Country code: 690
Country name: Tonga, Country code: 676
Country name: Trinidad and Tobago, Country code: 1
Country name: Tuvalu, Country code: 688
Country name: U.S. Outlying Islands, Country code: 0
Country name: Saint Vincent and the Grenadines, Country code: 1
Country name: British Virgin Islands, Country code: 1
Country name: U.S. Virgin Islands, Country code: 1
Country name: Vanuatu, Country code: 678
Country name: Samoa, Country code: 685
Country name: Zimbabwe, Country code: 263
Country name: Latin America, Country code: 0
Country name: Costa Rica, Country code: 506
Country name: Cuba, Country code: 53
Country name: Dominican Republic, Country code: 1
Country name: Ecuador, Country code: 593
Country name: Equatorial Guinea, Country code: 240
Country name: Guatemala, Country code: 502
Country name: Honduras, Country code: 504
Country name: Mexico, Country code: 52
Country name: Nicaragua, Country code: 505
Country name: Panama, Country code: 507
Country name: Peru, Country code: 51
Country name: Paraguay, Country code: 595
Country name: El Salvador, Country code: 503
Country name: Uruguay, Country code: 598
Country name: Venezuela, Country code: 58
Country name: Estonia, Country code: 372
Country name: Iran, Country code: 98
Country name: Guinea, Country code: 224
Country name: Faroe Islands, Country code: 298
Country name: Burkina Faso, Country code: 226
Country name: Benin, Country code: 229
Country name: Saint Barthélemy, Country code: 590
Country name: Congo (DRC), Country code: 243
Country name: Central African Republic, Country code: 236
Country name: Congo, Country code: 242
Country name: Côte d’Ivoire, Country code: 225
Country name: Gabon, Country code: 241
Country name: French Guiana, Country code: 594
Country name: Guadeloupe, Country code: 590
Country name: Haiti, Country code: 509
Country name: Monaco, Country code: 377
Country name: Saint Martin, Country code: 590
Country name: Martinique, Country code: 596
Country name: New Caledonia, Country code: 687
Country name: French Polynesia, Country code: 689
Country name: Saint Pierre and Miquelon, Country code: 508
Country name: Réunion, Country code: 262
Country name: Wallis and Futuna, Country code: 681
Country name: Mayotte, Country code: 262
Country name: Croatia, Country code: 385
Country name: Hungary, Country code: 36
Country name: Iceland, Country code: 354
Country name: San Marino, Country code: 378
Country name: Vatican City, Country code: 39
Country name: Japan, Country code: 81
Country name: Georgia, Country code: 995
Country name: Cabo Verde, Country code: 238
Country name: Kazakhstan, Country code: 7
Country name: Cambodia, Country code: 855
Country name: North Korea, Country code: 850
Country name: Korea, Country code: 82
Country name: Kyrgyzstan, Country code: 996
Country name: Laos, Country code: 856
Country name: Angola, Country code: 244
Country name: Lithuania, Country code: 370
Country name: Latvia, Country code: 371
Country name: Mozambique, Country code: 258
Country name: Macedonia, FYRO, Country code: 389
Country name: Mongolia, Country code: 976
Country name: Myanmar, Country code: 95
Country name: Norway, Country code: 47
Country name: Svalbard and Jan Mayen, Country code: 47
Country name: Nepal, Country code: 977
Country name: Aruba, Country code: 297
Country name: Bonaire, Sint Eustatius and Saba, Country code: 599
Country name: Curaçao, Country code: 599
Country name: Suriname, Country code: 597
Country name: Poland, Country code: 48
Country name: Portugal, Country code: 351
Country name: Guinea-Bissau, Country code: 245
Country name: São Tomé and Príncipe, Country code: 239
Country name: Timor-Leste, Country code: 670
Country name: Romania, Country code: 40
Country name: Moldova, Country code: 373
Country name: Ukraine, Country code: 380
Country name: Sri Lanka, Country code: 94
Country name: Slovakia, Country code: 421
Country name: Albania, Country code: 355
Country name: Kosovo, Country code: 383
Country name: Montenegro, Country code: 382
Country name: Serbia, Country code: 381
Country name: Åland Islands, Country code: 358
Country name: Tajikistan, Country code: 992
Country name: Thailand, Country code: 66
Country name: Turkmenistan, Country code: 993
Country name: Turkey, Country code: 90
Country name: Uzbekistan, Country code: 998
Country name: Vietnam, Country code: 84
Country name: Taiwan, Country code: 886
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment