Skip to content

OnixLabs.Core.IValueComparable

matthew edited this page May 27, 2026 · 1 revision

IValueComparable<T> Interface

Defines an extension to the default IComparable<T> and IComparable interfaces, adding the equality, inequality, greater-than, greater-than-or-equal, less-than, and less-than-or-equal operators required for full value semantics.

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

Definition

public interface IValueComparable<in T> : IComparable<T>, IComparable where T : IValueComparable<T>

Type Parameters

Parameter Description
T The underlying type of the objects to compare. It is declared contravariant (in T) and constrained by the curiously recurring template pattern, where T : IValueComparable<T>, binding the static abstract operators to the concrete implementing type.

Remarks

IValueComparable<T> augments IComparable<T> and the non-generic IComparable with a full set of relational 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 all six operators along with the inherited CompareTo(T?) and CompareTo(object?) methods.

Because ordering implies equality, a type that implements IValueComparable<T> typically also implements IValueEquatable<T> (or otherwise overrides Equals and GetHashCode) so that the == and != operators remain consistent with CompareTo returning zero.

Note Unlike the nullable operators on IValueEquatable<T>, the relational operators declared here take non-nullable T parameters.

Examples

The following value-semantic type is both comparable and equatable. It implements CompareTo and provides the full set of relational operators. Implementing both IValueEquatable<T> and IValueComparable<T> keeps equality and ordering consistent.

using System;
using OnixLabs.Core;

public readonly struct Version : IValueEquatable<Version>, IValueComparable<Version>
{
    public Version(int major, int minor)
    {
        Major = major;
        Minor = minor;
    }

    public int Major { get; }
    public int Minor { get; }

    public bool Equals(Version other) => Major == other.Major && Minor == other.Minor;

    public override bool Equals(object? obj) => obj is Version other && Equals(other);

    public override int GetHashCode() => HashCode.Combine(Major, Minor);

    public int CompareTo(Version other)
    {
        int result = Major.CompareTo(other.Major);
        return result != 0 ? result : Minor.CompareTo(other.Minor);
    }

    public int CompareTo(object? obj)
    {
        if (obj is null) return 1;
        if (obj is Version other) return CompareTo(other);
        throw new ArgumentException($"Object must be of type {nameof(Version)}.", nameof(obj));
    }

    public static bool operator ==(Version left, Version right) => left.Equals(right);
    public static bool operator !=(Version left, Version right) => !left.Equals(right);
    public static bool operator >(Version left, Version right) => left.CompareTo(right) > 0;
    public static bool operator >=(Version left, Version right) => left.CompareTo(right) >= 0;
    public static bool operator <(Version left, Version right) => left.CompareTo(right) < 0;
    public static bool operator <=(Version left, Version right) => left.CompareTo(right) <= 0;
}

var v1 = new Version(1, 2);
var v2 = new Version(1, 5);

Console.WriteLine(v1 < v2);   // True
Console.WriteLine(v1 == v2);  // False

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) Performs a greater-than comparison between two object instances.
operator >=(T, T) Performs a greater-than-or-equal comparison between two object instances.
operator <(T, T) Performs a less-than comparison between two object instances.
operator <=(T, T) Performs a less-than-or-equal 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 left is equal to right; 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 left is not equal to right; otherwise, false.


operator >(T, T)

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

Performs a greater-than 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 left is greater than right; otherwise, false.


operator >=(T, T)

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

Performs a greater-than-or-equal 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 left is greater than or equal to right; otherwise, false.


operator <(T, T)

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

Performs a less-than 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 left is less than right; otherwise, false.


operator <=(T, T)

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

Performs a less-than-or-equal 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 left is less than or equal to right; otherwise, false.

See Also

ONIXLabs .NET Library


Clone this wiki locally