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
IsSubdivision
Gets a value indicating whether the code names a subdivision rather than a bare country.
public bool IsSubdivision { get; }
Property Value
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
Methods
Contains(TerritoryCode)
Determines whether this code is, or is the parent of, the supplied code.
public bool Contains(TerritoryCode other)
Parameters
otherTerritoryCodeThe candidate code.
Returns
Equals(TerritoryCode)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(TerritoryCode other)
Parameters
otherTerritoryCodeAn object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
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
valuestringThe code to parse.
Returns
- TerritoryCode
The parsed code.
Exceptions
- ArgumentNullException
valueis null.- FormatException
valueis 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
Returns
- IReadOnlyList<TerritoryCode>
The parsed codes in source order, or an empty list when
valueis 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
valuestringThe code to parse, which may be null.
resultTerritoryCodeWhen this method returns, the parsed code, or the default value on failure.
Returns
Operators
operator ==(TerritoryCode, TerritoryCode)
Determines whether two codes are equal.
public static bool operator ==(TerritoryCode left, TerritoryCode right)
Parameters
leftTerritoryCodeThe first code.
rightTerritoryCodeThe second code.
Returns
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
valuestringThe 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
codeTerritoryCodeThe 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
leftTerritoryCodeThe first code.
rightTerritoryCodeThe second code.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |