Repository navigation
OnixLabs.Core.ResultEqualityComparer
Represents an equality comparer for comparing Result<T> instances.
| Namespace | OnixLabs.Core |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → ResultEqualityComparer<T>
|
| Implements | IEqualityComparer<Result<T>> |
public sealed class ResultEqualityComparer<T>(EqualityComparer<T>? valueComparer = null) : IEqualityComparer<Result<T>>| Parameter | Description |
|---|---|
T |
The underlying type of the Result<T> instances being compared. |
| Constructor | Description |
|---|---|
ResultEqualityComparer(EqualityComparer<T>?) |
Initializes a new comparer with an optional value comparer. |
public ResultEqualityComparer(EqualityComparer<T>? valueComparer = null)Initializes a new ResultEqualityComparer<T>. When valueComparer is null, the comparer falls back to the default equality comparer for T when comparing the values of two successful results.
| Parameter | Type | Description |
|---|---|---|
valueComparer |
EqualityComparer<T>? |
The comparer used to compare the underlying values of two Success<T> instances. Pass null to use EqualityComparer<T>.Default. |
ResultEqualityComparer<T> is used internally by Result<T>.Equals(Result<T>?) via the Default instance. The comparison rules are:
- Reference-equal instances (including two
nullreferences) are equal. - A single
nullis never equal to a non-nullinstance. - Two
Failure<T>instances are equal when theirExceptionvalues are equal (viaobject.Equals). - Two
Success<T>instances are equal when theirValues are equal under the value comparer. - A
Success<T>is never equal to aFailure<T>.
Pass a custom EqualityComparer<T> to the constructor to control how the values of two successful results are compared; for example, to use case-insensitive comparison for Result<string>.
| Field | Type | Description |
|---|---|---|
Default |
ResultEqualityComparer<T> |
The default comparer instance, using the default value comparer for T. |
public static readonly ResultEqualityComparer<T> DefaultThe default ResultEqualityComparer<T> instance, constructed with no explicit value comparer (equivalent to passing EqualityComparer<T>.Default).
| Method | Returns | Description |
|---|---|---|
Equals(Result<T>?, Result<T>?) |
bool |
Determines whether two results are equal. |
GetHashCode(Result<T>) |
int |
Returns a hash code for a result. |
public bool Equals(Result<T>? x, Result<T>? y)Determines whether the two specified results are equal, applying the rules described in Remarks.
| Parameter | Type | Description |
|---|---|---|
x |
Result<T>? |
The first result to compare. |
y |
Result<T>? |
The second result to compare. |
Returns — true if the results are equal; otherwise, false.
public int GetHashCode(Result<T> obj)Returns a hash code for the specified result. For a Success<T> the value's hash code under the value comparer is used (or 0 when the value is null); for a Failure<T> (or any other result type) the result's own GetHashCode() is used.
| Parameter | Type | Description |
|---|---|---|
obj |
Result<T> |
The result for which to obtain a hash code. |
Returns — A 32-bit signed integer hash code.
using OnixLabs.Core;
Result<int> a = Result<int>.Success(42);
Result<int> b = Result<int>.Success(42);
Result<int> c = Result<int>.Success(99);
bool eq = ResultEqualityComparer<int>.Default.Equals(a, b); // true
bool ne = ResultEqualityComparer<int>.Default.Equals(a, c); // falseusing OnixLabs.Core;
// Case-insensitive comparison of string results
var comparer = new ResultEqualityComparer<string>(StringComparer.OrdinalIgnoreCase as EqualityComparer<string>);
Result<string> x = Result<string>.Success("Hello");
Result<string> y = Result<string>.Success("hello");
bool eq = comparer.Equals(x, y); // trueusing OnixLabs.Core;
var results = new[]
{
Result<int>.Success(1),
Result<int>.Success(2),
Result<int>.Success(1),
};
var distinct = results.Distinct(ResultEqualityComparer<int>.Default).ToList();
// distinct contains Success(1) and Success(2)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