Money
Record Struct sealedpublic
Represents a monetary amount paired with an ISO 4217 currency code, ensuring that different currencies are never accidentally mixed in arithmetic.
Definition
Section titled Definitionpublic sealed record struct Money : System.IEquatable<SampleApi.Money> IEquatable<Money>
Inheritance
Section titled Inheritance IEquatable<Money>
classDiagram
direction TB
class Money {
<<record>>
}
class IEquatable~Money~ {
<<interface>>
}
IEquatable~Money~ <|.. MoneyRemarks
Section titled RemarksMoney is a value object implemented as a
readonly record struct to guarantee immutability and value-based equality. Using Money instead of a bare Decimal prevents subtle bugs where values in different currencies are summed or compared. The Amount is stored as a Decimal to avoid the floating-point precision issues inherent to Double in financial calculations.Constructors1
Section titled ConstructorsConstructor Section titled Constructor Money.Money( decimal Amount, string Currency)Represents a monetary amount paired with an ISO 4217 currency code, ensuring that different currencies are never accidentally mixed in arithmetic.
Parameters
Amount decimal The numeric value of the monetary amount.
Currency string The three-letter ISO 4217 currency code (e.g.,
"USD", "EUR", "GBP"). Remarks
Money is a value object implemented as a
readonly record struct to guarantee immutability and value-based equality. Using Money instead of a bare Decimal prevents subtle bugs where values in different currencies are summed or compared. The Amount is stored as a Decimal to avoid the floating-point precision issues inherent to Double in financial calculations.Examples
Creating and displaying monetary values:
var price = Money.Usd(29.99m);var shipping = Money.Eur(5.50m);
Console.WriteLine(price); // "29.99 USD"Console.WriteLine(shipping); // "5.50 EUR"Properties2
Section titled PropertiesCurrency Section titled Currency string string Money.CurrencyThe three-letter ISO 4217 currency code (e.g., "USD", "EUR", "GBP").
Methods3
Section titled MethodsMoney Money.Eur( decimal amount)Creates a Money value denominated in euros.
Parameters
amount decimal The euro amount.
string string Money.ToString()Returns a string representation in the format "100.00 USD".
Returns
string A formatted string of the monetary value. Examples
Section titled ExamplesCreating and displaying monetary values:
var price = Money.Usd(29.99m);var shipping = Money.Eur(5.50m);
Console.WriteLine(price); // "29.99 USD"Console.WriteLine(shipping); // "5.50 EUR"