Repository navigation
OnixLabs.Core.IValueEquatable
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> |
public interface IValueEquatable<T> : IEquatable<T> where T : IValueEquatable<T>| 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. |
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 inOnixLabs.Security.Cryptographyimplement it to guarantee a consistent equality surface across the library. A type that also requires ordering should implementIValueComparable<T>alongside this interface.
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| Operator | Description |
|---|---|
operator ==(T?, T?) |
Performs an equality comparison between two object instances. |
operator !=(T?, T?) |
Performs an inequality 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 the left instance is equal to the right instance; 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 the left instance is not equal to the right instance; 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