Back to results for “Boxing & Unboxing”
C# › Equality: Equals, GetHashCode & IEquatable

How does implementing IEquatable<T> improve on just overriding Equals(object), and why is it recommended alongside the override?

Intermediate

Answer

IEquatable<T>.Equals(T other) provides a STRONGLY-TYPED equality method that avoids the boxing (for structs) and the type-checking/casting overhead that Equals(object) requires — many generic collections and LINQ methods automatically prefer the IEquatable<T> overload when available, giving better performance.
public struct Point : IEquatable<Point> {
  public int X, Y;
  public bool Equals(Point other) => X == other.X && Y == other.Y; // strongly-typed, no boxing
  public override bool Equals(object? obj) => obj is Point p && Equals(p); // delegates to the typed version
  public override int GetHashCode() => HashCode.Combine(X, Y);
}
Real-world example Implementing IEquatable<T> on a value type used heavily in a List<T>.Contains() or Dictionary key scenario for better performance.

Common follow-ups: Why does implementing IEquatable<T> matter MORE for structs than for classes, performance-wise?