Class Country
- Namespace
- HeroKit.Globalization
- Assembly
- HeroKit.dll
A country and its rules. Predefined countries are in Countries.
public sealed class Country
- Inheritance
-
Country
- Inherited Members
- Extension Methods
Remarks
Every rule is optional. Methods that need a rule the country does not have throw NotSupportedException. To support another country, implement the rule interfaces, create an instance and register it with Register(Country).
Constructors
Country(string, string, Language)
A country and its rules. Predefined countries are in Countries.
public Country(string code, string name, Language language)
Parameters
codestringThe ISO 3166-1 alpha-2 code, for example "TR".
namestringThe English name, for example "Türkiye".
languageLanguageThe primary language, used for region names.
Remarks
Every rule is optional. Methods that need a rule the country does not have throw NotSupportedException. To support another country, implement the rule interfaces, create an instance and register it with Register(Country).
Properties
Banks
Gets the banks and how to find them from an IBAN, or null.
public IBankDirectory? Banks { get; init; }
Property Value
Code
Gets the ISO 3166-1 alpha-2 code.
public string Code { get; }
Property Value
Currency
Gets the national currency, or null.
public Currency? Currency { get; init; }
Property Value
Districts
Gets the second-level administrative units, for example the 973 Turkish districts (ilçe).
public IReadOnlyList<District> Districts { get; init; }
Property Value
Holidays
Gets the public holiday calendar, or null.
public IHolidayCalendar? Holidays { get; init; }
Property Value
IbanRules
Gets the national IBAN rules (length and fixed positions), or null. IBAN validation applies them to IBANs with this country code.
public IIdentifierRules? IbanRules { get; init; }
Property Value
Language
Gets the primary language.
public Language Language { get; }
Property Value
Name
Gets the English name.
public string Name { get; }
Property Value
NationalIds
Gets the national identity number rules, or null.
public IIdentifierRules? NationalIds { get; init; }
Property Value
PhoneNumbers
Gets the phone number rules, or null.
public IPhoneNumberRules? PhoneNumbers { get; init; }
Property Value
PostalCodes
Gets the postal code rules, or null.
public IIdentifierRules? PostalCodes { get; init; }
Property Value
Regions
Gets the first-level administrative regions, for example the 81 Turkish provinces.
public IReadOnlyList<Region> Regions { get; init; }
Property Value
TaxIds
Gets the tax identifier rules, or null.
public IIdentifierRules? TaxIds { get; init; }
Property Value
TimeZone
Gets the time zone of the country, or null when the country has more than one (for example the United States).
public TimeZoneInfo? TimeZone { get; init; }
Property Value
VehiclePlates
Gets the vehicle plate rules, or null.
public IVehiclePlateRules? VehiclePlates { get; init; }
Property Value
Methods
FindDistrict(int)
Finds a district by its official code.
public District? FindDistrict(int code)
Parameters
codeintThe district code, for example 1757.
Returns
- District?
The district, or null.
FindDistrict(string?, string?)
Finds a district by its name within a region, ignoring case and diacritics.
public District? FindDistrict(string? regionCodeOrName, string? districtName)
Parameters
regionCodeOrNamestringThe region code or name, for example "34" or "istanbul".
districtNamestringThe district name, for example "kadikoy".
Returns
- District?
The district, or null if the region or the district is unknown.
Examples
Countries.Turkey.FindDistrict("istanbul", "kadikoy")?.Name; // "Kadıköy"
FindRegion(int)
Finds a region by its code.
public Region? FindRegion(int code)
Parameters
codeintThe region code.
Returns
- Region?
The region, or null.
FindRegion(string?)
Finds a region by its code ("34", "06"), its abbreviation ("CA") or its name, ignoring case and diacritics ("istanbul", "Sanliurfa").
public Region? FindRegion(string? codeOrName)
Parameters
codeOrNamestringThe code or the name.
Returns
- Region?
The region, or null.
Examples
Countries.Turkey.FindRegion("34")?.Name; // "İstanbul"
Countries.Turkey.FindRegion("sanliurfa")?.Code; // 63
Countries.UnitedStates.FindRegion("ca")?.Name; // "California"
GetDistricts(int)
Returns the districts of a region.
public IReadOnlyList<District> GetDistricts(int regionCode)
Parameters
regionCodeintThe region code, for example 34.
Returns
- IReadOnlyList<District>
The districts in the order of Districts; empty for an unknown region.
Examples
Countries.Turkey.GetDistricts(34).Count; // 39
GetDistricts(string?)
Returns the districts of a region found by FindRegion(string?).
public IReadOnlyList<District> GetDistricts(string? regionCodeOrName)
Parameters
regionCodeOrNamestringThe region code or name, for example "34" or "istanbul".
Returns
- IReadOnlyList<District>
The districts; empty for an unknown region.
Now(TimeProvider?)
Gets the current date and time in the country's TimeZone, also when the server runs in UTC.
public DateTimeOffset Now(TimeProvider? timeProvider = null)
Parameters
timeProviderTimeProviderThe clock, for tests; null uses System.
Returns
- DateTimeOffset
The current local date and time with its UTC offset.
Examples
Countries.Turkey.Now(); // 2026-10-19 09:30:00 +03:00 while the server clock shows 06:30 UTC
Exceptions
- NotSupportedException
The country has no single time zone.
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
Today(TimeProvider?)
Gets today's date in the country's TimeZone. Between midnight in Türkiye and 03:00 UTC, a server in UTC still shows yesterday.
public DateOnly Today(TimeProvider? timeProvider = null)
Parameters
timeProviderTimeProviderThe clock, for tests; null uses System.
Returns
- DateOnly
Today's date in the country.
Examples
Countries.Turkey.Today().IsBusinessDay(Countries.Turkey);
Exceptions
- NotSupportedException
The country has no single time zone.
WithAdditionalHolidays(IEnumerable<PublicHoliday>)
Returns a copy of the country whose holiday calendar also contains the given days, for example administrative leave ("idari izin") or company holidays. A given day replaces a holiday of the country on the same date. The country itself does not change.
public Country WithAdditionalHolidays(IEnumerable<PublicHoliday> holidays)
Parameters
holidaysIEnumerable<PublicHoliday>The days to add; set IsHalfDay for an afternoon off.
Returns
- Country
A copy with every other rule of this country.
Examples
Country turkey = Countries.Turkey.WithAdditionalHolidays([new(new DateOnly(2026, 5, 18), "İdari izin", false)]);
new DateOnly(2026, 5, 18).IsBusinessDay(turkey); // false
Exceptions
- NotSupportedException
The country has no holiday calendar.
- ArgumentException
A date is given more than once, or a name is empty.
- ArgumentOutOfRangeException
A date is outside the supported years of the calendar.
WithHolidays(IHolidayCalendar)
Returns a copy of the country with another holiday calendar. The country itself does not change.
public Country WithHolidays(IHolidayCalendar holidays)
Parameters
holidaysIHolidayCalendarThe holiday calendar of the copy.
Returns
- Country
A copy with every other rule of this country.