Skip to content

OnixLabs.Core.ComparableExtensions

Matthew Layton edited this page Nov 15, 2026 · 2 revisions

ComparableExtensions Class

Extension methods for IComparable<T> instances, providing a non-generic CompareTo(object) bridge and inclusive/exclusive range tests.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → ComparableExtensions

Definition

[EditorBrowsable(EditorBrowsableState.Never)]
public static class ComparableExtensions

Note This class is decorated with [EditorBrowsable(EditorBrowsableState.Never)], which keeps the container class itself out of IntelliSense lists while its extension methods remain fully usable on the receiver types. Bring the methods into scope with using OnixLabs.Core;.

Remarks

ComparableExtensions provides two paths for implementing the non-generic IComparable.CompareTo(object) contract on top of a generic IComparable<T> implementation, plus inclusive and exclusive range-check helpers for any self-comparable type.

Methods

Method Returns Description
CompareToObject<T>(this IComparable<T>, object?) int Implements the non-generic CompareTo(object) contract for a type where the implementer differs from the comparable parameter T (the F-bounded/CRTP path).
CompareToObject<T>(this T, object?) int Implements the non-generic CompareTo(object) contract for a self-comparable type (T : IComparable<T>).
IsWithinRangeInclusive<T>(this T, T, T) bool Determines whether the value falls within an inclusive range.
IsWithinRangeExclusive<T>(this T, T, T) bool Determines whether the value falls within an exclusive range.

CompareToObject<T>(this IComparable<T>, object?)

public static int CompareToObject<T>(this IComparable<T> left, object? right)

Implements the non-generic IComparable.CompareTo(object) contract on top of a generic IComparable<T> implementation, where the implementing type differs from the comparable parameter T. This is the F-bounded / CRTP path — for example, Enumeration<T> : IComparable<T> where T : Enumeration<T>.

Parameter Type Description
left IComparable<T> The current IComparable<T> instance.
right object? The object to compare against. May be null.

Returns — A positive value (1) when right is null; otherwise the signed comparison result from IComparable<T>.CompareTo(T).

Exceptions

Exception Condition
ArgumentException right is not null and is not of type T.

CompareToObject<T>(this T, object?)

public static int CompareToObject<T>(this T left, object? right) where T : IComparable<T>

Implements the non-generic IComparable.CompareTo(object) contract on top of a generic IComparable<T> implementation, where the implementing type is itself T. This is the self-comparable path — for example, Hash : IComparable<Hash>. It avoids boxing when T is a value type.

Parameter Type Description
left T The current T instance.
right object? The object to compare against. May be null.

Returns — A positive value (1) when right is null; otherwise the signed comparison result from IComparable<T>.CompareTo(T).

Exceptions

Exception Condition
ArgumentException right is not null and is not of type T.
public readonly struct Version : IComparable<Version>, IComparable
{
    private readonly int value;
    public Version(int value) => this.value = value;
    public int CompareTo(Version other) => value.CompareTo(other.value);
    public int CompareTo(object? obj) => this.CompareToObject(obj);
}

IsWithinRangeInclusive<T>(this T, T, T)

public static bool IsWithinRangeInclusive<T>(this T left, T minimum, T maximum) where T : IComparable<T>

Determines whether the current value falls within range, inclusive of the specified minimum and maximum values.

Parameter Type Description
left T The current T instance.
minimum T The inclusive minimum value.
maximum T The inclusive maximum value.

Returns — true if the value falls within range, inclusive of minimum and maximum; otherwise, false.

bool valid = 5.IsWithinRangeInclusive(1, 5); // True

IsWithinRangeExclusive<T>(this T, T, T)

public static bool IsWithinRangeExclusive<T>(this T left, T minimum, T maximum) where T : IComparable<T>

Determines whether the current value falls within range, exclusive of the specified minimum and maximum values.

Parameter Type Description
left T The current T instance.
minimum T The exclusive minimum value.
maximum T The exclusive maximum value.

Returns — true if the value falls within range, exclusive of minimum and maximum; otherwise, false.

bool valid = 5.IsWithinRangeExclusive(1, 5); // False (5 is excluded)

See Also

ONIXLabs .NET Library


Clone this wiki locally