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
Check if two Tuple Objects are equal in C#
To check if two Tuple objects are equal in C#, you can use the Equals() method or the equality operator (==). Tuples are compared element-wise, meaning all corresponding elements must be equal for the tuples to be considered equal.
Syntax
Following is the syntax for comparing tuples using the Equals() method −
bool result = tuple1.Equals(tuple2);
You can also use the equality operator −
bool result = tuple1 == tuple2;
How Tuple Equality Works
Tuple equality comparison follows these rules:
Both tuples must have the same number of elements
Each corresponding element must be equal using their
Equals()methodThe comparison is performed element by element from left to right
If any element pair is not equal, the entire comparison returns
false
Using Equals() Method
Example
using System;
public class Demo {
public static void Main(String[] args) {
var tuple1 = Tuple.Create(150, 400, 500, 700, 100, 1200, 1500);
var tuple2 = Tuple.Create(150, 400, 500, 700, 100, 1200, 1500);
Console.WriteLine("Is Tuple1 equal to Tuple2? = " + tuple1.Equals(tuple2));
}
}
The output of the above code is −
Is Tuple1 equal to Tuple2? = True
Nested Tuple Comparison
Example
using System;
public class Demo {
public static void Main(String[] args) {
var tuple1 = Tuple.Create(150, 400, 500, 700, 100, 1200, 1500, Tuple.Create(50, 100));
var tuple2 = Tuple.Create(150, 400, 500, 700, 100, 1200, 1500, Tuple.Create(100, 200));
Console.WriteLine("Is Tuple1 equal to Tuple2? = " + tuple1.Equals(tuple2));
// Same nested values
var tuple3 = Tuple.Create(150, 400, 500, 700, 100, 1200, 1500, Tuple.Create(50, 100));
Console.WriteLine("Is Tuple1 equal to Tuple3? = " + tuple1.Equals(tuple3));
}
}
The output of the above code is −
Is Tuple1 equal to Tuple2? = False Is Tuple1 equal to Tuple3? = True
Using Equality Operator
Example
using System;
public class Demo {
public static void Main(String[] args) {
var tuple1 = Tuple.Create("Hello", 42, true);
var tuple2 = Tuple.Create("Hello", 42, true);
var tuple3 = Tuple.Create("Hello", 42, false);
Console.WriteLine("tuple1 == tuple2: " + (tuple1 == tuple2));
Console.WriteLine("tuple1 == tuple3: " + (tuple1 == tuple3));
Console.WriteLine("tuple1 != tuple3: " + (tuple1 != tuple3));
}
}
The output of the above code is −
tuple1 == tuple2: True tuple1 == tuple3: False tuple1 != tuple3: True
Comparison Methods
| Method | Description | Example |
|---|---|---|
Equals() |
Instance method for equality comparison | tuple1.Equals(tuple2) |
== operator |
Static equality operator | tuple1 == tuple2 |
!= operator |
Static inequality operator | tuple1 != tuple2 |
Conclusion
Tuple equality in C# compares all corresponding elements using their Equals() methods. Both the Equals() method and equality operators (==, !=) provide the same element-wise comparison behavior, making it easy to check if two tuples contain identical values.
