CKTIS / C#
System.IEquatable<T> — strongly typed equality
System.IEquatable<T> defines Equals(T), an equality comparison with a strongly typed argument. A consistent implementation also covers Equals(object), GetHashCode(), and the == and != operators so that a custom type behaves predictably in collections.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
public readonly struct ItemId : System.IEquatable<ItemId>
{
public int Value { get; }
public ItemId(int value) => Value = value;
public bool Equals(ItemId other) => Value == other.Value;
}System.IEquatable<T> lets you define equality for a specific type; usually T is the implementing type, as in ItemId : IEquatable<ItemId>. Its Equals(T) method returns true when values are equal under your chosen rule. It does not define sort order — that is the role of IComparable<T>. The interface is available in .NET 10; the example requires no preview features.
Equals(T) should be reflexive, symmetric, and transitive, and should produce consistent results while the data is unchanged. For a class, comparison with null should return false. Also override Equals(object) and GetHashCode(), keeping them consistent with typed equality. The interface does not itself define the == and != operators; if you provide them, they must follow the same rule.
EqualityComparer<T>.Default uses IEquatable<T> when the type implements it. This lets generic collections use typed equality and avoids boxing structs during that comparison. A custom comparer supplied to a collection can apply a different rule. Explicitly casting a struct to object still causes boxing, as in the example's second call.
Equal values must return identical results from GetHashCode(), but identical hashes do not prove equality. Do not change equality- or hash-defining data while an object is a key in Dictionary<TKey,TValue> or an element of HashSet<T>. The example uses an immutable struct: two identifiers whose Value is 42 are equal, so HashSet<ItemId> rejects the duplicate.
Examples
#nullable enable
using System;
using System.Collections.Generic;
public readonly struct ItemId : IEquatable<ItemId>
{
public int Value { get; }
public ItemId(int value) => Value = value;
public bool Equals(ItemId other) => Value == other.Value;
public override bool Equals(object? obj) =>
obj is ItemId other && Equals(other);
public override int GetHashCode() => Value;
public static bool operator ==(ItemId left, ItemId right) =>
left.Equals(right);
public static bool operator !=(ItemId left, ItemId right) =>
!left.Equals(right);
}
public static class Program
{
public static void Main()
{
var first = new ItemId(42);
var same = new ItemId(42);
var ids = new HashSet<ItemId> { first };
Console.WriteLine(first.Equals(same));
Console.WriteLine(first.Equals((object)same));
Console.WriteLine(first == same);
Console.WriteLine(ids.Contains(same));
Console.WriteLine(ids.Add(same));
Console.WriteLine(ids.Count);
}
}Result
True True True True False 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.