Repository navigation
OnixLabs.Core.ComparableExtensions
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
|
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ComparableExtensionsNote 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 withusing OnixLabs.Core;.
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.
| 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. |
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. |
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);
}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); // Truepublic 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)- OnixLabs.Core
- Home
- OnixLabs.Core.ArrayExtensions
- OnixLabs.Core.DateTimeExtensions
- OnixLabs.Core.ISpanParsableExtensions
- OnixLabs.Core.ObjectExtensions
- OnixLabs.Core.RandomExtensions
- OnixLabs.Core.ReadOnlySequenceExtensions
- OnixLabs.Core.ReadOnlySpanExtensions
- OnixLabs.Core.StringExtensions
- OnixLabs.Core on NuGet
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions