Table of Contents

Class CountryCode

Namespace
Trellis.Primitives
Assembly
Trellis.Primitives.dll

ISO 3166-1 alpha-2 country code value object.

[JsonConverter(typeof(ParsableJsonConverter<CountryCode>))]
public class CountryCode : ScalarValueObject<CountryCode, string>, IComparable<ValueObject>, IComparable, IEquatable<ValueObject>, IConvertible, IFormattable, IScalarValue<CountryCode, string>, IParsable<CountryCode>
Inheritance
CountryCode
Implements
Inherited Members
Extension Methods

Remarks

Validation Rules (Opinionated):

  • Exactly 2 ASCII letters (ISO 3166-1 alpha-2 format) — Unicode letters such as German umlauts, Greek, or Cyrillic are rejected.
  • Normalized to uppercase

If these rules don't fit your domain (e.g., you need alpha-3 or numeric codes), create your own CountryCode value object using the ScalarValueObject<TSelf, T> base class.

Methods

Parse(string?, IFormatProvider?)

Parses a country code.

public static CountryCode Parse(string? s, IFormatProvider? provider)

Parameters

s string
provider IFormatProvider

Returns

CountryCode

TryCreate(string?, string?)

Attempts to create a country code. If fieldName is not provided, validation errors use "countryCode" as the field name.

public static Result<CountryCode> TryCreate(string? value, string? fieldName = null)

Parameters

value string

The string value to validate.

fieldName string

Optional field name for validation error messages. If not provided, defaults to "countryCode".

Returns

Result<CountryCode>

Success with the CountryCode if valid; Failure with Error.InvalidInput otherwise.

TryParse(string?, IFormatProvider?, out CountryCode)

Tries to parse a country code.

public static bool TryParse(string? s, IFormatProvider? provider, out CountryCode result)

Parameters

s string
provider IFormatProvider
result CountryCode

Returns

bool