Compare Tuples In C#

Tuples are useful in C# language when it comes to returning and working with a group of items. If you’re new to tuples, check out Tuples in C# article to learn more about tuples.
You can compare two tuples by using equality == and != operators. The process compares left side of the tuple members with the right side of the tuple members in order. The == operator stops evaluating once it finds that member of the left side does not match with the member on the right side.
Tuple comparison is available in C# 7.3 or later versions. If you try to use it in previous versions, you will see the following error message.
In case of == operator, if the left side member value does not match with the right side member value, it will stop comparing and return false. The following code snippet compares two tuples and checks if the values are equal.
  1. var tupleA = (one: 1, three: 3, five: 5);  
  2. var tupleB = (a: 1, b: 3, c: 5);  
  4. if (tupleA == tupleB)  
  5. Console.WriteLine("Tuples values are same.");  
  6. else  
  7. Console.WriteLine("Tuples values are different.");  
The != operator stops evaluating members as soon as one pair is equal. The following code snippet uses != operator to check if values are different.
  1. if (tupleA != tupleB)  
  2. Console.WriteLine("Tuples values are different.");  
  3. else  
  4. Console.WriteLine("Tuples values are same.");  
Tuples support implicit conversions. Means, you can compare an int value tuple with a long value tuple.