Skip to content

Tuple equality operators C# 7.3code reductionreadability

Compare tuples directly using `==` and `!=` operators.

C# 7.0 introduced tuples but they could not be compared directly using == and !=. You had to compare each element individually or rely on Equals.

C# 7.3 adds support for the == and != operators on tuples, performing element-wise comparison with implicit conversions where applicable.

Code

C#
var point1 = (x: 1, y: 2);
var point2 = (x: 1, y: 2);

if (point1 == point2)
    Console.WriteLine("Same point");
C#
var point1 = (x: 1, y: 2);
var point2 = (x: 1, y: 2);

if (point1.x == point2.x && point1.y == point2.y)
    Console.WriteLine("Same point");

Notes

  • Comparison is performed element-wise from left to right and short-circuits on the first mismatch
  • Element names do not need to match — only the types and positions matter
  • Implicit conversions are applied per-element (e.g., int to long)
  • Nested tuples are compared recursively
  • Nullable tuples are also supported

More information