Provides strongly typed ISO 3166-1 country and region codes (Alpha-2, Alpha-3) generated at compile time from UN M49 numeric codes. Includes lookup service (ICountryCodeService). Lightweight and optimized for .NET 8 applications.
$ dotnet add package HawkN.Iso.CountriesHawkN.Iso.Countries provides ISO 3166-1 country codes (Alpha-2, Alpha-3), official names, numeric codes (UN M49), and validation services.
ISO 3166-1 country data with numeric codes from UN M49.TwoLetterCode and ThreeLetterCode enums are generated at compile-time.ValidationResult providing detailed feedback for code and name verification.dotnet add package HawkN.Iso.Countries
using HawkN.Iso.Countries;
using HawkN.Iso.Countries.Abstractions;
using HawkN.Iso.Countries.Models;
using HawkN.Iso.Countries.Extensions;
Register the service in your DI container:
using var host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddCountryCodeService();
})
.Build();
The service provides O(1) lookups via pre-indexed dictionaries and efficient partial searching.
var service = scope.ServiceProvider.GetRequiredService<ICountryCodeService>();
// Get all countries sorted by name
var countries = service.GetAll();
// Lookup by string (Supports Alpha-2, Alpha-3, or Numeric)
var germany = service.FindByCode("DE");
var austria = service.FindByCode("040");
// Lookup by Name
var france = service.FindByName("France");
// Strongly typed lookup
var uk = service.Get(CountryCode.TwoLetterCode.GB);
// Strongly typed lookup
var uk = service.Get(CountryCode.TwoLetterCode.GB);
// Scenario: User types "Republic" in a search box
var searchResults = service.SearchByName("Republic");
foreach (var country in searchResults)
{
// Will return:
// 1. Republic of Korea
// 2. Czech Republic
// 3. Lao People's Democratic Republic...
Console.WriteLine($"{country.Name} ({country.OfficialName})");
}
// Pro Tip: Use for suggestion lists
var suggestions = service.SearchByName("United")
.Select(c => c.Name)
.Take(5);
// Returns: ["United Arab Emirates", "United Kingdom", "United States", ...]
Check if a code or name is valid and retrieve the model simultaneously:
// Validate by Code
var result = service.ValidateByCode("US", out var country);
if (result.IsValid)
{
Console.WriteLine($"Found: {country.Name}");
}
// Validate by Name
var nameResult = service.ValidateByName("Unknown Land", out _);
if (!nameResult.IsValid)
{
Console.WriteLine($"Error: {nameResult.Reason}");
}
string input = "FRA";
// Direct conversion
var country = input.ToCountry(service);
// Quick check
if ("US".IsCountryCode(service))
{
// ...
}
// Quick validation
var validationResult = "US".ValidateAsCountryCode(countryCodeService, out var _);
if (validationResult.IsValid)
{
// ...
}
The library provides an easy way to display country flags using standard Unicode Emoji. This works without any external image assets and is perfect for lightweight UI components.
var country = service.Get(CountryCode.TwoLetterCode.FI);
// Get the emoji flag using the extension method
string flag = country.GetEmojiFlag();
Console.WriteLine($"{flag} {country.Name}");
// Output: 🇫🇮 Finland
See the country list with the link
Last updated at 25.12.2025.
CountryCode.TwoLetterCode – Enum for Alpha-2 codes (e.g., US, GB).CountryCode.ThreeLetterCode – Enum for Alpha-3 codes (e.g., USA, GBR).Country – Model containing Name, enums, string codes, and NumericCode.The source code of HawkN.Iso.Countries is licensed under the MIT License.
Country data (ISO 3166-1 and UN M49 numeric codes) is sourced from the UN Statistics Division – M49 standard
If you see ?? instead of flags in your console:
Console.OutputEncoding = System.Text.Encoding.UTF8;