Table of Contents

ExchangeRate<TBase, TQuote> Struct

Definition

Namespace
Bodu.Financial.ExchangeRates
Assembly
Bodu.Financial.dll
Package
Bodu.Financial 1.0.0
Source
ExchangeRate{T,T}.cs

Strongly-typed companion to ExchangeRate where the base and quote currencies are domain invariants encoded as type parameters.

public readonly struct ExchangeRate<TBase, TQuote> : IEquatable<ExchangeRate<TBase, TQuote>> where TBase : ICurrency where TQuote : ICurrency

Type Parameters

TBase

The source-currency tag.

TQuote

The destination-currency tag.

Implements
IEquatable<ExchangeRate<TBase, TQuote>>
Inherited Members
Extension Methods

Remarks

Use this typed form when an FX conversion's direction is fixed by the surrounding contract (settlement workflows, ledger-to-ledger transfers, account-currency mappings). Direction errors that ExchangeRate can surface only at runtime (mismatched From / To against the caller's expectation) become compile errors when the typed form is used. Bridge to and from the runtime form via ToRuntime() and FromRuntime(ExchangeRate).

Constructors

ExchangeRate(decimal, DateOnly, string, bool)

Initializes a new instance of the ExchangeRate<TBase, TQuote> struct.

public ExchangeRate(decimal rate, DateOnly date, string provider, bool isInverted = false)

Parameters

rate decimal

The multiplier that converts a TBase amount to TQuote.

date DateOnly

The calendar date on which the rate was observed.

provider string

The non-empty identifier of the publishing source.

isInverted bool

true when the rate was derived from the reverse pair; otherwise false.

Exceptions

ArgumentNullException

provider is null.

ArgumentException

provider is empty or white-space.

ArgumentOutOfRangeException

rate is zero or negative.

Properties

Date

Gets the calendar date on which the rate was observed.

public DateOnly Date { get; }

Property Value

DateOnly

The observation date.

FromIsoCode

Gets the ISO 4217 alphabetic code of the source currency, derived from TBase.

public string FromIsoCode { get; }

Property Value

string

The three-character uppercase ASCII source-currency code.

IsInverted

Gets a value indicating whether this rate was derived from the reverse pair.

public bool IsInverted { get; }

Property Value

bool

true when Rate is the reciprocal of an originally published reverse-direction rate; otherwise false.

Provider

Gets the non-empty identifier of the publishing source.

public string Provider { get; }

Property Value

string

The provider identifier.

Rate

Gets the multiplier that converts a TBase amount to TQuote.

public decimal Rate { get; }

Property Value

decimal

A strictly positive multiplier.

ToIsoCode

Gets the ISO 4217 alphabetic code of the destination currency, derived from TQuote.

public string ToIsoCode { get; }

Property Value

string

The three-character uppercase ASCII destination-currency code.

Methods

Convert(Money<TBase>, MidpointRounding)

Converts a TBase amount to TQuote, rounding the result to the destination currency's minor-unit precision.

public Money<TQuote> Convert(Money<TBase> amount, MidpointRounding rounding = MidpointRounding.ToEven)

Parameters

amount Money<TBase>

The amount in TBase to convert.

rounding MidpointRounding

The midpoint-rounding rule applied at the destination precision.

Returns

Money<TQuote>

The converted amount in TQuote.

Equals(ExchangeRate<TBase, TQuote>)

Determines whether this rate equals other by its public fields. The internal observed rate is excluded so two rates that report the same multiplier, date, provider, and inversion compare equal regardless of how each was constructed.

public bool Equals(ExchangeRate<TBase, TQuote> other)

Parameters

other ExchangeRate<TBase, TQuote>

The rate to compare with.

Returns

bool

true when the public fields match; 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.

From(decimal, DateOnly, string)

Creates a strongly-typed exchange rate from the supplied parameters.

public static ExchangeRate<TBase, TQuote> From(decimal rate, DateOnly date, string provider)

Parameters

rate decimal

The multiplier; must be strictly positive.

date DateOnly

The observation date.

provider string

The non-empty source identifier.

Returns

ExchangeRate<TBase, TQuote>

The constructed rate.

Exceptions

ArgumentNullException

provider is null.

ArgumentException

provider is empty or white-space.

ArgumentOutOfRangeException

rate is zero or negative.

FromRuntime(ExchangeRate)

Adopts a runtime-tagged ExchangeRate as the typed form when the runtime currencies match TBase and TQuote.

public static ExchangeRate<TBase, TQuote> FromRuntime(ExchangeRate rate)

Parameters

rate ExchangeRate

The runtime-tagged rate.

Returns

ExchangeRate<TBase, TQuote>

The strongly-typed equivalent.

Exceptions

InvalidOperationException

The runtime rate's From or To does not match the currency of TBase or TQuote respectively.

GetHashCode()

Returns a hash code over the public fields, consistent with Equals(ExchangeRate<TBase, TQuote>).

public override int GetHashCode()

Returns

int

The hash code.

Inverse()

Returns the reciprocal rate that converts in the opposite direction.

public ExchangeRate<TQuote, TBase> Inverse()

Returns

ExchangeRate<TQuote, TBase>

An ExchangeRate<TBase, TQuote> whose Rate is 1 / this.Rate.

ToRuntime()

Bridges this typed rate to the runtime-tagged ExchangeRate record.

public ExchangeRate ToRuntime()

Returns

ExchangeRate

An ExchangeRate carrying the same fields with the currencies resolved from TBase and TQuote.

ToString()

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string

The fully qualified type name.

Operators

operator ==(ExchangeRate<TBase, TQuote>, ExchangeRate<TBase, TQuote>)

public static bool operator ==(ExchangeRate<TBase, TQuote> left, ExchangeRate<TBase, TQuote> right)

Parameters

left ExchangeRate<TBase, TQuote>
right ExchangeRate<TBase, TQuote>

Returns

bool

operator !=(ExchangeRate<TBase, TQuote>, ExchangeRate<TBase, TQuote>)

public static bool operator !=(ExchangeRate<TBase, TQuote> left, ExchangeRate<TBase, TQuote> right)

Parameters

left ExchangeRate<TBase, TQuote>
right ExchangeRate<TBase, TQuote>

Returns

bool

Applies to

ProductVersions
.NET8, 10