Skip to content

OnixLabs.Core.IValueEquatable

matthew edited this page May 27, 2026 · 1 revision

IValueEquatable<T> Interface

Defines an extension to the default IEquatable<T> interface, adding the equality and inequality operators required for full value semantics.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Implements IEquatable<T>

Definition

public interface IValueEquatable<T> : IEquatable<T> where T : IValueEquatable<T>

Type Parameters

Parameter Description
T The underlying type of the objects to compare. The self-referential constraint where T : IValueEquatable<T> is the curiously recurring template pattern, which binds the static abstract operators to the concrete implementing type.

Remarks

IValueEquatable<T> augments IEquatable<T> with the == and != operators, declared as static abstract members. By building on C# 11 static abstract interface members, the interface makes the operators part of the contract, so that any implementing type is guaranteed to support the natural operator syntax in addition to the method-based API.

Implementers must provide both operators along with the inherited Equals(T?) method. Conventionally, a type that implements this interface also overrides object.Equals(object?) and object.GetHashCode() so that equality is consistent across all usage paths.

Both operators accept nullable parameters, allowing null operands on either side; a correct implementation treats two null references as equal.

Note This interface declares the operator contract; it does not provide an implementation. It is most commonly consumed indirectly — for example, Result<T> and the cryptographic primitives in OnixLabs.Security.Cryptography implement it to guarantee a consistent equality surface across the library. A type that also requires ordering should implement IValueComparable<T> alongside this interface.

Examples

The following value-semantic type compares solely by its data. It implements Equals(T?) from IEquatable<T>, overrides Equals(object?) and GetHashCode, and supplies the two operators required by IValueEquatable<T>.

using System;
using OnixLabs.Core;

public sealed class Money : IValueEquatable<Money>
{
    public Money(decimal amount, string currency)
    {
        Amount = amount;
        Currency = currency;
    }

    public decimal Amount { get; }
    public string Currency { get; }

    public bool Equals(Money? other) =>
        other is not null && Amount == other.Amount && Currency == other.Currency;

    public override bool Equals(object? obj) => Equals(obj as Money);

    public override int GetHashCode() => HashCode.Combine(Amount, Currency);

    public static bool operator ==(Money? left, Money? right) =>
        Equals(left, right);

    public static bool operator !=(Money? left, Money? right) =>
        !Equals(left, right);
}

var a = new Money(10m, "GBP");
var b = new Money(10m, "GBP");

Console.WriteLine(a == b);       // True  (value equality)
Console.WriteLine(a.Equals(b));  // True

Operators

Operator Description
operator ==(T?, T?) Performs an equality comparison between two object instances.
operator !=(T?, T?) Performs an inequality comparison between two object instances.

operator ==(T?, T?)

public static abstract bool operator ==(T? left, T? right);

Performs an equality comparison between two object instances.

Parameter Type Description
left T? The left-hand instance to compare.
right T? The right-hand instance to compare.

Returns — true if the left instance is equal to the right instance; otherwise, false.


operator !=(T?, T?)

public static abstract bool operator !=(T? left, T? right);

Performs an inequality comparison between two object instances.

Parameter Type Description
left T? The left-hand instance to compare.
right T? The right-hand instance to compare.

Returns — true if the left instance is not equal to the right instance; otherwise, false.

See Also

ONIXLabs .NET Library


Clone this wiki locally