Skip to content

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.
Namespace SampleApi
Assembly SampleApi
Package v1.0.0.0
public sealed record struct Money
: System.IEquatable<SampleApi.Money>
IEquatable<Money>
Implements
IEquatable<Money>
classDiagram
    direction TB
    class Money {
        <<record>>
    }
    class IEquatable~Money~ {
        <<interface>>
    }
    IEquatable~Money~ <|.. Money
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.
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.

Amount decimal
The numeric value of the monetary amount.
Currency string
The three-letter ISO 4217 currency code (e.g., "USD", "EUR", "GBP").
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.

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"
decimal Money.Amount

The numeric value of the monetary amount.

string Money.Currency

The three-letter ISO 4217 currency code (e.g., "USD", "EUR", "GBP").

Money Money.Eur(
decimal amount)

Creates a Money value denominated in euros.

amount decimal
The euro amount.
Money A new Money instance with currency set to "EUR".
ToString Section titled ToString override
string
string Money.ToString()

Returns a string representation in the format "100.00 USD".

string A formatted string of the monetary value.
Money Money.Usd(
decimal amount)

Creates a Money value denominated in US dollars.

amount decimal
The dollar amount.
Money A new Money instance with currency set to "USD".

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"