Skip to content

OnixLabs.Core.OptionalEqualityComparer

matthew edited this page May 27, 2026 · 1 revision

OptionalEqualityComparer<T> Class

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>>

Definition

public sealed class OptionalEqualityComparer<T>(EqualityComparer<T>? valueEqualityComparer = null)
    : IEqualityComparer<Optional<T>>
    where T : notnull

Type Parameters

Parameter Description
T The underlying type of the Optional<T> instances being compared. Constrained to notnull.

Remarks

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 null references, or the same reference, are equal.
  • Two None<T> instances are equal.
  • A Some<T> and a None<T> are never equal.
  • Two Some<T> instances are equal when their underlying values are equal, as determined by the supplied valueEqualityComparer, or EqualityComparer<T>.Default if 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).

Examples

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)

Fields

Field Type Description
Default OptionalEqualityComparer<T> The default comparer instance.

Default

public static readonly OptionalEqualityComparer<T> Default

The 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>.

Methods

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.

Equals(Optional<T>?, Optional<T>?)

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.

GetHashCode(Optional<T>)

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.

See Also

ONIXLabs .NET Library


Clone this wiki locally