Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to compare two tuples in C#?
Tuple comparison was introduced in C# 7.3, allowing you to easily compare two tuples using equality operators. Tuples are compared element-wise, meaning each corresponding element in both tuples must be equal for the tuples to be considered equal.
Syntax
Following is the syntax for comparing tuples using the equality operator −
bool result = tuple1 == tuple2;
You can also use the inequality operator −
bool result = tuple1 != tuple2;
How Tuple Comparison Works
Tuple comparison in C# follows these rules:
Tuples must have the same number of elements to be compared.
Each corresponding element is compared using the
==operator.Element names are ignored during comparison ? only values matter.
The comparison is performed element-wise from left to right.
Using Equality Operator
Example
using System;
class Program {
public static void Main() {
var tuple1 = (x: 1, y: 2);
var tuple2 = (p: 1, q: 2);
var tuple3 = (a: 1, b: 3);
if (tuple1 == tuple2)
Console.WriteLine("tuple1 and tuple2 are equal");
else
Console.WriteLine("tuple1 and tuple2 are not equal");
if (tuple1 == tuple3)
Console.WriteLine("tuple1 and tuple3 are equal");
else
Console.WriteLine("tuple1 and tuple3 are not equal");
}
}
The output of the above code is −
tuple1 and tuple2 are equal tuple1 and tuple3 are not equal
Using Inequality Operator
Example
using System;
class Program {
public static void Main() {
var first = (name: "John", age: 25);
var second = (person: "John", years: 30);
if (first != second) {
Console.WriteLine("The tuples are different");
Console.WriteLine($"First tuple: {first}");
Console.WriteLine($"Second tuple: {second}");
}
}
}
The output of the above code is −
The tuples are different First tuple: (John, 25) Second tuple: (John, 30)
Comparing Tuples with Different Types
Example
using System;
class Program {
public static void Main() {
var intTuple = (1, 2);
var doubleTuple = (1.0, 2.0);
var stringTuple = ("Hello", "World");
var mixedTuple = (1, "Test");
Console.WriteLine($"intTuple == doubleTuple: {intTuple == doubleTuple}");
Console.WriteLine($"stringTuple: {stringTuple}");
Console.WriteLine($"mixedTuple: {mixedTuple}");
// Different arity tuples cannot be compared
var twoElement = (1, 2);
var threeElement = (1, 2, 3);
Console.WriteLine($"Two element tuple: {twoElement}");
Console.WriteLine($"Three element tuple: {threeElement}");
}
}
The output of the above code is −
intTuple == doubleTuple: True stringTuple: (Hello, World) mixedTuple: (1, Test) Two element tuple: (1, 2) Three element tuple: (1, 2, 3)
Key Rules
| Rule | Description |
|---|---|
| Same Arity | Tuples must have the same number of elements to be comparable. |
| Element Names Ignored | Only element values are compared, not their names. |
| Type Compatibility | Corresponding elements must be of compatible types for comparison. |
| Element-wise Comparison | Each element is compared using the == operator. |
Conclusion
Tuple comparison in C# 7.3+ uses element-wise comparison with the == and != operators. Element names are ignored during comparison, and only the values matter. Both tuples must have the same number of elements and compatible types for comparison to work.
