Sic.CountryInfos 1.0.0

dotnet add package Sic.CountryInfos --version 1.0.0
                    
NuGet\Install-Package Sic.CountryInfos -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sic.CountryInfos" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sic.CountryInfos" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Sic.CountryInfos" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sic.CountryInfos --version 1.0.0
                    
#r "nuget: Sic.CountryInfos, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sic.CountryInfos@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sic.CountryInfos&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Sic.CountryInfos&version=1.0.0
                    
Install as a Cake Tool

Country Info Generator

The BCL (Base Class Library) only has the RegionInfo and CultureInfo classes to represent countries and cultures. Unfortunately, the data representation (using mainly strings) can make handling conversions between different formats error-prone.

This project aims to create a source generator that generates a C# class for each country, with properties for the country's name, ISO code, and other relevant information. It uses the BCL's RegionInfo and CultureInfo class to get the data. Additionally, a second nuget package is present that contains the generated data.

Installation

You may install the package from NuGet:

dotnet add package Sic.CountryInfos

If you prefer to use the source generator, with which the namespace and the contained data can be configured, see Source Generator.

Usage

There are three enums representing the country's data:

  • Country: Contains the english name of the country (spaces and special characters omitted).
  • CountryIso2Code: Contains the ISO 3166-1 alpha-2 code of the country.
  • CountryIso3Code: Contains the ISO 3166-1 alpha-3 code of the country.

All enums use the GeoId (which is the same as the RegionInfo's GeoId) as the enum value. Additionally, there is a IsSameCountry-extension method that checks if two (different) enums represent the same country, e.g.:

bool isSameCountry = Country.Germany.IsSameCountry(CountryIso2CodeName.DE); // true

Additionally, there is a LocaleCode enum that contains the locale code of the countries. The values represent the LCID (Locale Identifier) value.

    /// <summary>
    /// Afrikaans (South Africa) - af-ZA
    /// </summary>
    af_ZA = 1078,
    /// <summary>
    /// Amharic (Ethiopia) - am-ET
    /// </summary>
    am_ET = 1118

All this information is additionally present in the CountryInfo class. An instance of this class can be created by using the CountryInfo.Get-method:

CountryInfo germany = CountryInfo.Get(Country.Germany);
// or
germany = CountryInfo.Get(CountryIso2Code.DE);
// or
germany = CountryInfo.Get(CountryIso3Code.DEU);
// or
germany = CountryInfo.Get(LocaleCode.de_DE);

The CountryInfo can than be used to get the country's name, ISO codes, and other information.

germany.EnglishName; // Germany as string
Country country = germany.Country; // Country.Germany
CountryIso2Code iso2Code = germany.TwoLetterIsoCode; // CountryIso2CodeName.DE
CountryIso3Code iso3Code = germany.ThreeLetterIsoCode; // CountryIso3CodeName.DEU
IReadOnlyList<LocaleCode> supportedLocales = germany.SupportedLocales; // [LocaleCode.de_DE, LocaleCode.dsb_DE, ...]

Additionally, the CountryInfo class contains a All-property that contains a list of all countries.

There are also extensions for the Country, CountryIso2Code, CountryIso3Code, and LocaleCode enums that allow getting the CountryInfo instance directly:

CountryInfo germany = Country.Germany.GetCountryInfo();

Source Generator

You might want to use the source generator, if you would like to

  • change the root namespace of the generated classes
  • filter the countries (and its locales) that should be generated

⚠ As the source generator uses the CultureInfo implementation to get the data, the output is dependent on your installed culture info data on your OS.

Install the generator via

dotnet add package Sic.CountryInfos.SourceGenerator

Filter Countries

You can filter the countries that should be generated by providing a list of either a Country.Countries.txt, CountryIso2Code.Countries.txt, or CountryIso3Code.Countries.txt enum names, e.g. for CountryIso2Code.Countries.txt:

DE
AT
BE
SE

The file has to be added as Additional File in the project file:

<ItemGroup>
    <None Remove="CountryIso2Code.Countries.txt"/>
    <AdditionalFiles Include="CountryIso2Code.Countries.txt"/>
</ItemGroup>

The source generator will then generate all enums only for the countries (and its locales) that are listed in the file.

Change Root Namespace

On default, the generator will generate the classes in the root namespace of your project. If you want to change the namespace, you can add the SicCountryInfosCustomNamespace property to your project file:

<ItemGroup>
       <CompilerVisibleProperty Include="SicCountryInfosCustomNamespace" />
</ItemGroup>

<PropertyGroup>
   <SicCountryInfosCustomNamespace>My.Custom.Namespace</SicCountryInfosCustomNamespace>
</PropertyGroup>
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 3,658 3/22/2025
0.10.0 525 3/15/2025