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() method

  • The comparison is performed element by element from left to right

  • If any element pair is not equal, the entire comparison returns false

Tuple Equality Comparison (1, 2, 3) == (1, 2, 3) True (1, 2, 3) == (1, 2, 4) 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.

Updated on: 2026-03-17T07:04:36+05:30

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements