Repository navigation
OnixLabs.Core.IValueComparable
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
|
public interface IValueComparable<in T> : IComparable<T>, IComparable where T : IValueComparable<T>| 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. |
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-nullableTparameters.
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| 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. |
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.
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.
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.
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.
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.
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.
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