Table of Contents

TerritoryCode Struct

Definition

Namespace
Bodu.Globalization.Calendar
Assembly
Bodu.Globalization.Calendar.dll
Package
Bodu.Globalization.Calendar 1.0.1
Source
TerritoryCode.cs

Represents a validated territory code composed of an ISO 3166-1 alpha-2 country and an optional subdivision (for example AU or AU-NSW), with parent/child containment semantics matching the resolution engine.

public readonly struct TerritoryCode : IEquatable<TerritoryCode>
Implements
Inherited Members
Extension Methods

Examples

TerritoryCode nsw = TerritoryCode.Parse("AU-NSW");
TerritoryCode au = nsw.Parent;          // AU
bool contains = au.Contains(nsw);        // true - a country contains its subdivisions

// The implicit string conversion feeds the resolution surface directly.
IReadOnlyList<NotableDate> dates = service.Resolve(2026, nsw);

// Lenient parsing for untrusted input.
if (TerritoryCode.TryParse(userInput, out TerritoryCode code))
    dates = service.Resolve(2026, code);

Remarks

The engine accepts territories as plain strings; this value type is an opt-in convenience that parses, normalizes, and decomposes a code, and interoperates with the string surface through an implicit conversion to its canonical form. A country-only code is a parent of every subdivision sharing its country: AU contains AU-NSW, mirroring how a subnational query matches a rule scoped to its parent territory.

The default value is empty (IsEmpty is true); it parses no code, contains nothing, and renders as the empty string.

Properties

Country

Gets the country part of the code.

public string Country { get; }

Property Value

string

The uppercase two-letter country, or the empty string for the default value.

IsEmpty

Gets a value indicating whether this is the default (empty) code.

public bool IsEmpty { get; }

Property Value

bool

true when no code is held; otherwise false.

IsSubdivision

Gets a value indicating whether the code names a subdivision rather than a bare country.

public bool IsSubdivision { get; }

Property Value

bool

true when a subdivision is present; otherwise false.

Parent

Gets the parent country code for a subdivision.

public TerritoryCode Parent { get; }

Property Value

TerritoryCode

The country-only TerritoryCode for a subdivision, the same code when it is already country-only, or the default value when this code is empty.

Subdivision

Gets the subdivision part of the code.

public string? Subdivision { get; }

Property Value

string

The uppercase subdivision, or null when the code is country-only.

Methods

Contains(TerritoryCode)

Determines whether this code is, or is the parent of, the supplied code.

public bool Contains(TerritoryCode other)

Parameters

other TerritoryCode

The candidate code.

Returns

bool

true when the codes are equal, or this code is a bare country and other is a subdivision of it; otherwise false.

Equals(TerritoryCode)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(TerritoryCode other)

Parameters

other TerritoryCode

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

Parse(string)

Parses a territory code, throwing when it is malformed.

public static TerritoryCode Parse(string value)

Parameters

value string

The code to parse.

Returns

TerritoryCode

The parsed code.

Exceptions

ArgumentNullException

value is null.

FormatException

value is not a two-letter country optionally followed by a hyphen and a one-to-three alphanumeric subdivision.

ParseList(string?)

Parses a comma-separated list of territory codes.

public static IReadOnlyList<TerritoryCode> ParseList(string? value)

Parameters

value string

The comma-separated codes, which may be null.

Returns

IReadOnlyList<TerritoryCode>

The parsed codes in source order, or an empty list when value is null, empty, or white space. Blank entries between commas are ignored.

Remarks

This parser is strict: every non-blank entry must be a valid code, otherwise the call throws. Use TryParse(string?, out TerritoryCode) per entry when leniency is required.

Exceptions

FormatException

Any non-blank entry is not a valid territory code.

ToString()

Returns the canonical string form of the code.

public override string ToString()

Returns

string

The country, the country and subdivision joined by a hyphen, or the empty string when default.

TryParse(string?, out TerritoryCode)

Attempts to parse a territory code without throwing.

public static bool TryParse(string? value, out TerritoryCode result)

Parameters

value string

The code to parse, which may be null.

result TerritoryCode

When this method returns, the parsed code, or the default value on failure.

Returns

bool

true when parsing succeeds; otherwise false.

Operators

operator ==(TerritoryCode, TerritoryCode)

Determines whether two codes are equal.

public static bool operator ==(TerritoryCode left, TerritoryCode right)

Parameters

left TerritoryCode

The first code.

right TerritoryCode

The second code.

Returns

bool

true when the codes are equal; otherwise false.

explicit operator TerritoryCode(string)

Converts a string to a TerritoryCode, throwing when the string is not a valid code.

public static explicit operator TerritoryCode(string value)

Parameters

value string

The code to convert.

Returns

TerritoryCode

The parsed code.

implicit operator string(TerritoryCode)

Converts a TerritoryCode to its canonical string form.

public static implicit operator string(TerritoryCode code)

Parameters

code TerritoryCode

The code to convert.

Returns

string

The canonical code string, accepted directly by the string-based resolution surface.

operator !=(TerritoryCode, TerritoryCode)

Determines whether two codes are not equal.

public static bool operator !=(TerritoryCode left, TerritoryCode right)

Parameters

left TerritoryCode

The first code.

right TerritoryCode

The second code.

Returns

bool

true when the codes differ; otherwise false.

Applies to

ProductVersions
.NET8, 10

See Also