Browse
Sample.LibCore.DataMoney
.md
struct

Money

publicreadonly
namespace Core.Data; public readonly struct Money : IEquatable<Money>

An immutable amount of money in a specific currency.

Arithmetic is only defined for amounts in the same currency.

  • Amounts are rounded to 2 decimal places.
  • Currency codes follow ISO 4217.

Constructors

#public Money(decimal amount, string currency = "EUR")

Creates a new amount.

amount—The amount.

currency—The ISO 4217 currency code, e.g. "EUR".

Fields

#public static readonly Money Zero;

A zero amount in euros.

Properties

#public decimal Amount { get; }

Gets the amount.

#public string Currency { get; init; }

Gets the ISO 4217 currency code.

Methods

#public bool Equals(Money other)

Indicates whether the current object is equal to another object of the same type.

other—An object to compare with this object.

returns—true if the current object is equal to the other parameter; otherwise, false.

#public override bool Equals(object? obj)

Indicates whether this instance and a specified object are equal.

obj—The object to compare with the current instance.

returns—true if obj and this instance are the same type and represent the same value; otherwise, false.

#public override int GetHashCode()

Returns the hash code for this instance.

returns—A 32-bit signed integer that is the hash code for this instance.

Operators

#public static Money operator +(Money left, Money right)

Adds two amounts.

left—The first amount.

right—The second amount.

returns—The sum of left and right.

throws— InvalidOperationException: The currencies differ.

#public static implicit operator Money(decimal amount)

Converts a decimal to an amount in euros.

amount—The amount.