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
TBaseThe source-currency tag.
TQuoteThe 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
ratedecimalThe multiplier that converts a
TBaseamount toTQuote.dateDateOnlyThe calendar date on which the rate was observed.
providerstringThe non-empty identifier of the publishing source.
isInvertedbooltrue when the rate was derived from the reverse pair; otherwise false.
Exceptions
- ArgumentNullException
provideris null.- ArgumentException
provideris empty or white-space.- ArgumentOutOfRangeException
rateis 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
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
amountMoney<TBase>The amount in
TBaseto convert.roundingMidpointRoundingThe 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
otherExchangeRate<TBase, TQuote>The rate to compare with.
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
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
ratedecimalThe multiplier; must be strictly positive.
dateDateOnlyThe observation date.
providerstringThe non-empty source identifier.
Returns
- ExchangeRate<TBase, TQuote>
The constructed rate.
Exceptions
- ArgumentNullException
provideris null.- ArgumentException
provideris empty or white-space.- ArgumentOutOfRangeException
rateis 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
rateExchangeRateThe 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
TBaseorTQuoterespectively.
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
TBaseandTQuote.
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
leftExchangeRate<TBase, TQuote>rightExchangeRate<TBase, TQuote>
Returns
operator !=(ExchangeRate<TBase, TQuote>, ExchangeRate<TBase, TQuote>)
public static bool operator !=(ExchangeRate<TBase, TQuote> left, ExchangeRate<TBase, TQuote> right)
Parameters
leftExchangeRate<TBase, TQuote>rightExchangeRate<TBase, TQuote>
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |