Repository navigation
OnixLabs.Core.OptionalEqualityComparer
Represents an equality comparer for comparing Optional<T> instances by value.
| Namespace | OnixLabs.Core |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → OptionalEqualityComparer<T>
|
| Implements | IEqualityComparer<Optional<T>> |
public sealed class OptionalEqualityComparer<T>(EqualityComparer<T>? valueEqualityComparer = null)
: IEqualityComparer<Optional<T>>
where T : notnull| Parameter | Description |
|---|---|
T |
The underlying type of the Optional<T> instances being compared. Constrained to notnull. |
OptionalEqualityComparer<T> defines the value-equality semantics used by Optional<T>.Equals and the equality operators on Optional<T>. It compares two optional values by case first, then by underlying value:
- Two
nullreferences, or the same reference, are equal. - Two
None<T>instances are equal. - A
Some<T>and aNone<T>are never equal. - Two
Some<T>instances are equal when their underlying values are equal, as determined by the suppliedvalueEqualityComparer, orEqualityComparer<T>.Defaultif none was supplied.
The primary constructor accepts an optional EqualityComparer<T> used to compare the underlying values. Pass a custom comparer to override the default value-equality behaviour. The Default static field exposes a shared instance that uses EqualityComparer<T>.Default; this is the comparer used internally by Optional<T>.
OptionalEqualityComparer<T> implements IEqualityComparer<Optional<T>>, so it can be passed directly to any API that accepts a custom equality comparer (for example, Dictionary<Optional<T>, V> or LINQ's Distinct/GroupBy).
using OnixLabs.Core;
using System;
using System.Collections.Generic;
// Case-insensitive comparison of the underlying string values.
var comparer = new OptionalEqualityComparer<string>(
EqualityComparer<string>.Create((x, y) => string.Equals(x, y, StringComparison.OrdinalIgnoreCase)));
Optional<string> a = "HELLO";
Optional<string> b = "hello";
bool equal = comparer.Equals(a, b); // true
// The default comparer is used by Optional<T> equality.
bool defaultEqual = OptionalEqualityComparer<int>.Default.Equals(
Optional<int>.Some(1),
Optional<int>.Some(1)); // true
// Use as a dictionary key comparer.
var dict = new Dictionary<Optional<string>, int>(comparer);
dict[Optional<string>.Some("KEY")] = 1;
Console.WriteLine(dict[Optional<string>.Some("key")]); // 1 (case-insensitive match)| Field | Type | Description |
|---|---|---|
Default |
OptionalEqualityComparer<T> |
The default comparer instance. |
public static readonly OptionalEqualityComparer<T> DefaultThe default OptionalEqualityComparer<T> instance, which compares underlying values using EqualityComparer<T>.Default. This is the comparer used by Optional<T>.Equals and the == / != operators on Optional<T>.
| Method | Returns | Description |
|---|---|---|
Equals(Optional<T>?, Optional<T>?) |
bool |
Determines whether two optional values are equal. |
GetHashCode(Optional<T>) |
int |
Returns a hash code for the specified optional value. |
public bool Equals(Optional<T>? x, Optional<T>? y)Determines whether two Optional<T> instances are equal, following the case-and-value semantics described in the Remarks.
| Parameter | Type | Description |
|---|---|---|
x |
Optional<T>? |
The first optional value to compare. |
y |
Optional<T>? |
The second optional value to compare. |
Returns — true if x and y are equal; otherwise, false.
public int GetHashCode(Optional<T> obj)Returns a hash code for the specified Optional<T> instance. For a Some<T>, returns the hash code of its underlying value (computed using the configured value comparer or EqualityComparer<T>.Default); for a None<T>, returns 0.
| Parameter | Type | Description |
|---|---|---|
obj |
Optional<T> |
The optional value for which to obtain a hash code. |
Returns — A 32-bit signed integer hash code.
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