Country data library for NET8

Country Data library for NET8

Today, I’m going to release a simple cross-platform offline country data library for NET8. The library is based on the ISO-3166-1 standard. Based on the operating system, using Unicode*, you can display the flag of the country. On Windows, the emoji displays the shortcode of the country, not the flag.

The library has the flags of the countries in SVG format. The flags can be displayed in two different types: Square and Wide.

The source code of the library is available on GitHub.

Install Library

To install the library in your project, open the Package Manager and type the following command:

Install-Package PSC.CSharp.CountryData

Also, you can use the .NET CLI to install the library. Type the following command:

dotnet add package PSC.CSharp.CountryData

Usage

Here is an example how to use the library in your project.

Initialize the CountryData

// loads all Country Data via the constructor (You can initialize this once as a singleton)
var countryHelper = new CountryHelper();

Get the list of countries

foreach (var country in countries) 
    Console.WriteLine(country);

Get the list of regions in a country by country code

var regions = countryHelper.GetRegionByCountryCode("IT");
foreach (var region in regions)
    Console.WriteLine(region.Name);

Using lambda for custom queries

GetCountryData() returns an IEnumerable<Country> which can be queried with Lambda for a more flexible usage.

var data = countryHelper.GetCountryData();

Get the list of countries by their Names

This code displays the name of the countries using a foreach.

var countries = data.Select(c => c.CountryName).ToList();
foreach (var country in countries)
    Console.WriteLine(country);

Here the code to use Linq to filter the list.

data.Where(x => x.CountryShortCode == "US")
    .Select(r=>r.Regions).FirstOrDefault()
    .ToList();

Get the flag for a country

In the library, I added a method to get the flag of a country. The flags are in SVG format. The type of the flag can be Square or Wide.

var flag = helper.GetFlagByCountryCode("GB", FlagType.Square);

List of supported ISO-3166-1 country codes

Here is the list of supported ISO-3166-1 country codes, their code point pairs and Emoji flags. Based on the operating system, you see different Emoji.
In Windows, you see the short name of the country. In iOS and Android, the flag of the correspondent country is displayed.

ISOEmojiUnicodeName
AD🇦🇩U+1F1E6 U+1F1E9Andorra
AE🇦🇪U+1F1E6 U+1F1EAUnited Arab Emirates
AF🇦🇫U+1F1E6 U+1F1EBAfghanistan
AG🇦🇬U+1F1E6 U+1F1ECAntigua and Barbuda
AI🇦🇮U+1F1E6 U+1F1EEAnguilla
AL🇦🇱U+1F1E6 U+1F1F1Albania
AM🇦🇲U+1F1E6 U+1F1F2Armenia
AO🇦🇴U+1F1E6 U+1F1F4Angola
AQ🇦🇶U+1F1E6 U+1F1F6Antarctica
AR🇦🇷U+1F1E6 U+1F1F7Argentina
AS🇦🇸U+1F1E6 U+1F1F8American Samoa
AT🇦🇹U+1F1E6 U+1F1F9Austria
AU🇦🇺U+1F1E6 U+1F1FAAustralia
AW🇦🇼U+1F1E6 U+1F1FCAruba
AX🇦🇽U+1F1E6 U+1F1FDÅland Islands
AZ🇦🇿U+1F1E6 U+1F1FFAzerbaijan
BA🇧🇦U+1F1E7 U+1F1E6Bosnia and Herzegovina
BB🇧🇧U+1F1E7 U+1F1E7Barbados
BD🇧🇩U+1F1E7 U+1F1E9Bangladesh
BE🇧🇪U+1F1E7 U+1F1EABelgium
BF🇧🇫U+1F1E7 U+1F1EBBurkina Faso
BG🇧🇬U+1F1E7 U+1F1ECBulgaria
BH🇧🇭U+1F1E7 U+1F1EDBahrain
BI🇧🇮U+1F1E7 U+1F1EEBurundi
BJ🇧🇯U+1F1E7 U+1F1EFBenin
BL🇧🇱U+1F1E7 U+1F1F1Saint Barthélemy
BM🇧🇲U+1F1E7 U+1F1F2Bermuda
BN🇧🇳U+1F1E7 U+1F1F3Brunei Darussalam
BO🇧🇴U+1F1E7 U+1F1F4Bolivia
BQ🇧🇶U+1F1E7 U+1F1F6Bonaire, Sint Eustatius and Saba
BR🇧🇷U+1F1E7 U+1F1F7Brazil
BS🇧🇸U+1F1E7 U+1F1F8Bahamas
BT🇧🇹U+1F1E7 U+1F1F9Bhutan
BV🇧🇻U+1F1E7 U+1F1FBBouvet Island
BW🇧🇼U+1F1E7 U+1F1FCBotswana
BY🇧🇾U+1F1E7 U+1F1FEBelarus
BZ🇧🇿U+1F1E7 U+1F1FFBelize
CA🇨🇦U+1F1E8 U+1F1E6Canada
CC🇨🇨U+1F1E8 U+1F1E8Cocos (Keeling) Islands
CD🇨🇩U+1F1E8 U+1F1E9Congo
CF🇨🇫U+1F1E8 U+1F1EBCentral African Republic
CG🇨🇬U+1F1E8 U+1F1ECCongo
CH🇨🇭U+1F1E8 U+1F1EDSwitzerland
CI🇨🇮U+1F1E8 U+1F1EECôte D’Ivoire
CK🇨🇰U+1F1E8 U+1F1F0Cook Islands
CL🇨🇱U+1F1E8 U+1F1F1Chile
CM🇨🇲U+1F1E8 U+1F1F2Cameroon
CN🇨🇳U+1F1E8 U+1F1F3China
CO🇨🇴U+1F1E8 U+1F1F4Colombia
CR🇨🇷U+1F1E8 U+1F1F7Costa Rica
CU🇨🇺U+1F1E8 U+1F1FACuba
CV🇨🇻U+1F1E8 U+1F1FBCape Verde
CW🇨🇼U+1F1E8 U+1F1FCCuraçao
CX🇨🇽U+1F1E8 U+1F1FDChristmas Island
CY🇨🇾U+1F1E8 U+1F1FECyprus
CZ🇨🇿U+1F1E8 U+1F1FFCzech Republic
DE🇩🇪U+1F1E9 U+1F1EAGermany
DJ🇩🇯U+1F1E9 U+1F1EFDjibouti
DK🇩🇰U+1F1E9 U+1F1F0Denmark
DM🇩🇲U+1F1E9 U+1F1F2Dominica
DO🇩🇴U+1F1E9 U+1F1F4Dominican Republic
DZ🇩🇿U+1F1E9 U+1F1FFAlgeria
EC🇪🇨U+1F1EA U+1F1E8Ecuador
EE🇪🇪U+1F1EA U+1F1EAEstonia
EG🇪🇬U+1F1EA U+1F1ECEgypt
EH🇪🇭U+1F1EA U+1F1EDWestern Sahara
ER🇪🇷U+1F1EA U+1F1F7Eritrea
ES🇪🇸U+1F1EA U+1F1F8Spain
ET🇪🇹U+1F1EA U+1F1F9Ethiopia
FI🇫🇮U+1F1EB U+1F1EEFinland
FJ🇫🇯U+1F1EB U+1F1EFFiji
FK🇫🇰U+1F1EB U+1F1F0Falkland Islands (Malvinas)
FM🇫🇲U+1F1EB U+1F1F2Micronesia
FO🇫🇴U+1F1EB U+1F1F4Faroe Islands
FR🇫🇷U+1F1EB U+1F1F7France
GA🇬🇦U+1F1EC U+1F1E6Gabon
GB🇬🇧U+1F1EC U+1F1E7United Kingdom
GD🇬🇩U+1F1EC U+1F1E9Grenada
GE🇬🇪U+1F1EC U+1F1EAGeorgia
GF🇬🇫U+1F1EC U+1F1EBFrench Guiana
GG🇬🇬U+1F1EC U+1F1ECGuernsey
GH🇬🇭U+1F1EC U+1F1EDGhana
GI🇬🇮U+1F1EC U+1F1EEGibraltar
GL🇬🇱U+1F1EC U+1F1F1Greenland
GM🇬🇲U+1F1EC U+1F1F2Gambia
GN🇬🇳U+1F1EC U+1F1F3Guinea
GP🇬🇵U+1F1EC U+1F1F5Guadeloupe
GQ🇬🇶U+1F1EC U+1F1F6Equatorial Guinea
GR🇬🇷U+1F1EC U+1F1F7Greece
GS🇬🇸U+1F1EC U+1F1F8South Georgia
GT🇬🇹U+1F1EC U+1F1F9Guatemala
GU🇬🇺U+1F1EC U+1F1FAGuam
GW🇬🇼U+1F1EC U+1F1FCGuinea-Bissau
GY🇬🇾U+1F1EC U+1F1FEGuyana
HK🇭🇰U+1F1ED U+1F1F0Hong Kong
HM🇭🇲U+1F1ED U+1F1F2Heard Island and Mcdonald Islands
HN🇭🇳U+1F1ED U+1F1F3Honduras
HR🇭🇷U+1F1ED U+1F1F7Croatia
HT🇭🇹U+1F1ED U+1F1F9Haiti
HU🇭🇺U+1F1ED U+1F1FAHungary
ID🇮🇩U+1F1EE U+1F1E9Indonesia
IE🇮🇪U+1F1EE U+1F1EAIreland
IL🇮🇱U+1F1EE U+1F1F1Israel
IM🇮🇲U+1F1EE U+1F1F2Isle of Man
IN🇮🇳U+1F1EE U+1F1F3India
IO🇮🇴U+1F1EE U+1F1F4British Indian Ocean Territory
IQ🇮🇶U+1F1EE U+1F1F6Iraq
IR🇮🇷U+1F1EE U+1F1F7Iran
IS🇮🇸U+1F1EE U+1F1F8Iceland
IT🇮🇹U+1F1EE U+1F1F9Italy
JE🇯🇪U+1F1EF U+1F1EAJersey
JM🇯🇲U+1F1EF U+1F1F2Jamaica
JO🇯🇴U+1F1EF U+1F1F4Jordan
JP🇯🇵U+1F1EF U+1F1F5Japan
KE🇰🇪U+1F1F0 U+1F1EAKenya
KG🇰🇬U+1F1F0 U+1F1ECKyrgyzstan
KH🇰🇭U+1F1F0 U+1F1EDCambodia
KI🇰🇮U+1F1F0 U+1F1EEKiribati
KM🇰🇲U+1F1F0 U+1F1F2Comoros
KN🇰🇳U+1F1F0 U+1F1F3Saint Kitts and Nevis
KP🇰🇵U+1F1F0 U+1F1F5North Korea
KR🇰🇷U+1F1F0 U+1F1F7South Korea
KW🇰🇼U+1F1F0 U+1F1FCKuwait
KY🇰🇾U+1F1F0 U+1F1FECayman Islands
KZ🇰🇿U+1F1F0 U+1F1FFKazakhstan
LA🇱🇦U+1F1F1 U+1F1E6Lao People’s Democratic Republic
LB🇱🇧U+1F1F1 U+1F1E7Lebanon
LC🇱🇨U+1F1F1 U+1F1E8Saint Lucia
LI🇱🇮U+1F1F1 U+1F1EELiechtenstein
LK🇱🇰U+1F1F1 U+1F1F0Sri Lanka
LR🇱🇷U+1F1F1 U+1F1F7Liberia
LS🇱🇸U+1F1F1 U+1F1F8Lesotho
LT🇱🇹U+1F1F1 U+1F1F9Lithuania
LU🇱🇺U+1F1F1 U+1F1FALuxembourg
LV🇱🇻U+1F1F1 U+1F1FBLatvia
LY🇱🇾U+1F1F1 U+1F1FELibya
MA🇲🇦U+1F1F2 U+1F1E6Morocco
MC🇲🇨U+1F1F2 U+1F1E8Monaco
MD🇲🇩U+1F1F2 U+1F1E9Moldova
ME🇲🇪U+1F1F2 U+1F1EAMontenegro
MF🇲🇫U+1F1F2 U+1F1EBSaint Martin (French Part)
MG🇲🇬U+1F1F2 U+1F1ECMadagascar
MH🇲🇭U+1F1F2 U+1F1EDMarshall Islands
MK🇲🇰U+1F1F2 U+1F1F0Macedonia
ML🇲🇱U+1F1F2 U+1F1F1Mali
MM🇲🇲U+1F1F2 U+1F1F2Myanmar
MN🇲🇳U+1F1F2 U+1F1F3Mongolia
MO🇲🇴U+1F1F2 U+1F1F4Macao
MP🇲🇵U+1F1F2 U+1F1F5Northern Mariana Islands
MQ🇲🇶U+1F1F2 U+1F1F6Martinique
MR🇲🇷U+1F1F2 U+1F1F7Mauritania
MS🇲🇸U+1F1F2 U+1F1F8Montserrat
MT🇲🇹U+1F1F2 U+1F1F9Malta
MU🇲🇺U+1F1F2 U+1F1FAMauritius
MV🇲🇻U+1F1F2 U+1F1FBMaldives
MW🇲🇼U+1F1F2 U+1F1FCMalawi
MX🇲🇽U+1F1F2 U+1F1FDMexico
MY🇲🇾U+1F1F2 U+1F1FEMalaysia
MZ🇲🇿U+1F1F2 U+1F1FFMozambique
NA🇳🇦U+1F1F3 U+1F1E6Namibia
NC🇳🇨U+1F1F3 U+1F1E8New Caledonia
NE🇳🇪U+1F1F3 U+1F1EANiger
NF🇳🇫U+1F1F3 U+1F1EBNorfolk Island
NG🇳🇬U+1F1F3 U+1F1ECNigeria
NI🇳🇮U+1F1F3 U+1F1EENicaragua
NL🇳🇱U+1F1F3 U+1F1F1Netherlands
NO🇳🇴U+1F1F3 U+1F1F4Norway
NP🇳🇵U+1F1F3 U+1F1F5Nepal
NR🇳🇷U+1F1F3 U+1F1F7Nauru
NU🇳🇺U+1F1F3 U+1F1FANiue
NZ🇳🇿U+1F1F3 U+1F1FFNew Zealand
OM🇴🇲U+1F1F4 U+1F1F2Oman
PA🇵🇦U+1F1F5 U+1F1E6Panama
PE🇵🇪U+1F1F5 U+1F1EAPeru
PF🇵🇫U+1F1F5 U+1F1EBFrench Polynesia
PG🇵🇬U+1F1F5 U+1F1ECPapua New Guinea
PH🇵🇭U+1F1F5 U+1F1EDPhilippines
PK🇵🇰U+1F1F5 U+1F1F0Pakistan
PL🇵🇱U+1F1F5 U+1F1F1Poland
PM🇵🇲U+1F1F5 U+1F1F2Saint Pierre and Miquelon
PN🇵🇳U+1F1F5 U+1F1F3Pitcairn
PR🇵🇷U+1F1F5 U+1F1F7Puerto Rico
PS🇵🇸U+1F1F5 U+1F1F8Palestinian Territory
PT🇵🇹U+1F1F5 U+1F1F9Portugal
PW🇵🇼U+1F1F5 U+1F1FCPalau
PY🇵🇾U+1F1F5 U+1F1FEParaguay
QA🇶🇦U+1F1F6 U+1F1E6Qatar
RE🇷🇪U+1F1F7 U+1F1EARéunion
RO🇷🇴U+1F1F7 U+1F1F4Romania
RS🇷🇸U+1F1F7 U+1F1F8Serbia
RU🇷🇺U+1F1F7 U+1F1FARussia
RW🇷🇼U+1F1F7 U+1F1FCRwanda
SA🇸🇦U+1F1F8 U+1F1E6Saudi Arabia
SB🇸🇧U+1F1F8 U+1F1E7Solomon Islands
SC🇸🇨U+1F1F8 U+1F1E8Seychelles
SD🇸🇩U+1F1F8 U+1F1E9Sudan
SE🇸🇪U+1F1F8 U+1F1EASweden
SG🇸🇬U+1F1F8 U+1F1ECSingapore
SH🇸🇭U+1F1F8 U+1F1EDSaint Helena, Ascension and Tristan Da Cunha
SI🇸🇮U+1F1F8 U+1F1EESlovenia
SJ🇸🇯U+1F1F8 U+1F1EFSvalbard and Jan Mayen
SK🇸🇰U+1F1F8 U+1F1F0Slovakia
SL🇸🇱U+1F1F8 U+1F1F1Sierra Leone
SM🇸🇲U+1F1F8 U+1F1F2San Marino
SN🇸🇳U+1F1F8 U+1F1F3Senegal
SO🇸🇴U+1F1F8 U+1F1F4Somalia
SR🇸🇷U+1F1F8 U+1F1F7Suriname
SS🇸🇸U+1F1F8 U+1F1F8South Sudan
ST🇸🇹U+1F1F8 U+1F1F9Sao Tome and Principe
SV🇸🇻U+1F1F8 U+1F1FBEl Salvador
SX🇸🇽U+1F1F8 U+1F1FDSint Maarten (Dutch Part)
SY🇸🇾U+1F1F8 U+1F1FESyrian Arab Republic
SZ🇸🇿U+1F1F8 U+1F1FFSwaziland
TC🇹🇨U+1F1F9 U+1F1E8Turks and Caicos Islands
TD🇹🇩U+1F1F9 U+1F1E9Chad
TF🇹🇫U+1F1F9 U+1F1EBFrench Southern Territories
TG🇹🇬U+1F1F9 U+1F1ECTogo
TH🇹🇭U+1F1F9 U+1F1EDThailand
TJ🇹🇯U+1F1F9 U+1F1EFTajikistan
TK🇹🇰U+1F1F9 U+1F1F0Tokelau
TL🇹🇱U+1F1F9 U+1F1F1Timor-Leste
TM🇹🇲U+1F1F9 U+1F1F2Turkmenistan
TN🇹🇳U+1F1F9 U+1F1F3Tunisia
TO🇹🇴U+1F1F9 U+1F1F4Tonga
TR🇹🇷U+1F1F9 U+1F1F7Turkey
TT🇹🇹U+1F1F9 U+1F1F9Trinidad and Tobago
TV🇹🇻U+1F1F9 U+1F1FBTuvalu
TW🇹🇼U+1F1F9 U+1F1FCTaiwan
TZ🇹🇿U+1F1F9 U+1F1FFTanzania
UA🇺🇦U+1F1FA U+1F1E6Ukraine
UG🇺🇬U+1F1FA U+1F1ECUganda
UM🇺🇲U+1F1FA U+1F1F2United States Minor Outlying Islands
US🇺🇸U+1F1FA U+1F1F8United States
UY🇺🇾U+1F1FA U+1F1FEUruguay
UZ🇺🇿U+1F1FA U+1F1FFUzbekistan
VA🇻🇦U+1F1FB U+1F1E6Vatican City
VC🇻🇨U+1F1FB U+1F1E8Saint Vincent and The Grenadines
VE🇻🇪U+1F1FB U+1F1EAVenezuela
VG🇻🇬U+1F1FB U+1F1ECVirgin Islands, British
VI🇻🇮U+1F1FB U+1F1EEVirgin Islands, U.S.
VN🇻🇳U+1F1FB U+1F1F3Viet Nam
VU🇻🇺U+1F1FB U+1F1FAVanuatu
WF🇼🇫U+1F1FC U+1F1EBWallis and Futuna
WS🇼🇸U+1F1FC U+1F1F8Samoa
YE🇾🇪U+1F1FE U+1F1EAYemen
YT🇾🇹U+1F1FE U+1F1F9Mayotte
ZA🇿🇦U+1F1FF U+1F1E6South Africa
ZM🇿🇲U+1F1FF U+1F1F2Zambia
ZW🇿🇼U+1F1FF U+1F1FCZimbabwe

Functions

FunctionReturnDescription
GetCountries()IEnumerableGets the list of all countries in the world
GetCountryByCodeCountryReturns a single Country Data by ShortCode
GetCountryData()IEnumerableGets the list of all countries in the world with their data
GetCountryEmojiFlagstringReturns the Emoji Flag of a country by ShortCode
GetRegionByCountryCodeListReturns the Regions of a country by ShortCode
GetJsonDatastringReturns the JSON Data of all countries in the world

Flags

FunctionReturnDescription
GetFlagByCountryCodestringReturns the Flag of a country by ShortCode
GetFlagDataFlagModelReturn the FlagModel of one country by ShortCode
GetNameByCountryCodestringReturns the Name of a country by ShortCode

Get a full SVG

The GetFlagByCountryCode returns only the content of the SVG file. What it doesn’t have is the XML declaration and the DOCTYPE declaration.
If you want to get the full SVG file, you can use the GetFullSVG function in the SVGFlags static class.

var svgFlag = SVGFlags.GetFullSVG(SVGFlags.lgbt_1, "300", "300");

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.