Skip to main content

Interface INumericValueObject<TSelf, TValue>

Namespace: AdCodicem.ValueObjects
Assembly: AdCodicem.ValueObjects.Abstractions.dll

A value object over a numeric underlying type, exposing arithmetic through the generic math interfaces.

public interface INumericValueObject<TSelf, TValue> : IValueObject<TSelf, TValue>, IValueObject<TValue>, IValueObject, IEquatable<TSelf>, IComparable<TSelf>, IComparable, ISpanParsable<TSelf>, IParsable<TSelf>, ISpanFormattable, IFormattable, IAdditionOperators<TSelf, TSelf, TSelf>, ISubtractionOperators<TSelf, TSelf, TSelf>, IComparisonOperators<TSelf, TSelf, bool>, IEqualityOperators<TSelf, TSelf, bool>, IMultiplyOperators<TSelf, TValue, TSelf>, IDivisionOperators<TSelf, TValue, TSelf> where TSelf : struct, INumericValueObject<TSelf, TValue> where TValue : struct, INumber<TValue>

Type Parameters

TSelf

The value object type itself.

TValue

Underlying numeric type.

Implements

IValueObject<TSelf, TValue>, IValueObject<TValue>, IValueObject, IEquatable<TSelf>, IComparable<TSelf>, IComparable, ISpanParsable<TSelf>, IParsable<TSelf>, ISpanFormattable, IFormattable, IAdditionOperators<TSelf, TSelf, TSelf>, ISubtractionOperators<TSelf, TSelf, TSelf>, IComparisonOperators<TSelf, TSelf, bool>, IEqualityOperators<TSelf, TSelf, bool>, IMultiplyOperators<TSelf, TValue, TSelf>, IDivisionOperators<TSelf, TValue, TSelf>

Remarks

Every arithmetic operator routes its result back through , so an operation that would produce an invalid value throws instead of silently escaping the type's rules — subtracting 30 from a Percentage of 20 is a bug, not a negative percentage.

Unary negation is emitted by the generator only for signed underlying types and is intentionally absent from this contract, so that value objects over unsigned integers can still take part in generic arithmetic. The same goes for dividing two value objects into a bare ratio: the operator is emitted on the concrete type, but declaring it here alongside division by a scalar would make the two instantiations of unifiable.

Properties

IsZero

Gets a value indicating whether the carried value is zero.

bool IsZero { get; }

Property Value

bool

One

Gets the value object representing one.

public static TSelf One { get; }

Property Value

TSelf

Exceptions

ValueObjectException

One is not a valid value for TSelf.

Zero

Gets the value object representing zero.

public static TSelf Zero { get; }

Property Value

TSelf

Exceptions

ValueObjectException

Zero is not a valid value for TSelf.

Methods

Abs(TSelf)

Returns the absolute value of value.

public static TSelf Abs(TSelf value)

Parameters

value TSelf

Value to take the absolute value of.

Returns

TSelf

The absolute value.

Exceptions

ValueObjectException

The absolute value is not valid for TSelf.

Max(TSelf, TSelf)

Returns the larger of two values.

public static TSelf Max(TSelf left, TSelf right)

Parameters

left TSelf

Left operand.

right TSelf

Right operand.

Returns

TSelf

The larger of the two operands.

Min(TSelf, TSelf)

Returns the smaller of two values.

public static TSelf Min(TSelf left, TSelf right)

Parameters

left TSelf

Left operand.

right TSelf

Right operand.

Returns

TSelf

The smaller of the two operands.

Sum(IEnumerable<TSelf>)

Computes the sum of a sequence of values.

public static TSelf Sum(IEnumerable<TSelf> values)

Parameters

values IEnumerable<TSelf>

Values to add up.

Returns

TSelf

The sum, or the value object built from TValue.Zero for an empty sequence.

Remarks

Accumulation happens on the underlying type and the result is validated once, so an intermediate total that momentarily leaves the valid range does not throw.