Checking if two ValueTuple<T1,T2,T3,T4,T5,T6> are equal or not in C#

ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It is already included in .NET Framework 4.7 or higher version. It allows you to store a data set that contains multiple values that may or may not be related to each other.
In ValueTuple<T1, T2, T3, T4, T5, T6>, you can check if two value tuples are identical or not by using the Equals method. Or in other words, we can say that this method will return the value which indicates whether the given ValueTuple<T1, T2, T3, T4, T5, T6> instance is equal to the specified object or not. It will return true if the given value tuples are equal, otherwise, return false. This method can be overloaded in two different ways:

  1. Equals(ValueTuple<T1, T2, T3, T4, T5, T6>) Method
  2. Equals(Object) Method

1. Equals(ValueTuple<T1, T2, T3, T4, T5, T6>) Method

The Equals(ValueTuple<T1, T2, T3, T4, T5, T6>) method is used to check whether the two ValueTuple<T1, T2, T3, T4, T5, T6 > instances are equal or not. It always returns true. The return type of this method is System.Boolean. In Equals(ValueTuple<T1, T2, T3, T4, T5, T6>) method the other parameter is considered to be equal to the current instance under the following conditions:

  • The components are of the same types as those of the current instance.
  • The elements are equal to those of the current instance and the equality of each element is determined by the default equality comparer.

Syntax:



public bool Equals (ValueTuple );

Return Type: The return type of this method is System.Boolean. It returns true if the given instance is equal to the specified instance. Otherwise, it returns false.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use of
// Equals(ValueTuple<T1, T2, T3, T4,
// T5, T6>) method
using System;
  
namespace exampleofvaluetuple {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // 5-ValueTuple
        var u1 = ValueTuple.Create(4, 3, 5, 3, 9);
        var u2 = ValueTuple.Create(4, 3, 5, 3, 9);
        Console.WriteLine("Result 1: {0}", u1.Equals(u2));
  
        // 6-ValueTuple
        var y1 = ValueTuple.Create(4, 32, 5, 3, 9, 98);
        var y2 = ValueTuple.Create(4, 3, 5, 3, 9, 76);
        Console.WriteLine("Result 2: {0}", y1.Equals(y2));
  
        // 7-ValueTuple
        var a1 = ValueTuple.Create(23, 45, 67, 89, 32, 56, 89);
        var a2 = ValueTuple.Create(23, 45, 67, 89, 32, 56, 89);
        Console.WriteLine("Result 3: {0}", a1.Equals(a2));
    }
}
}

chevron_right


Output:

Result 1: True
Result 2: False
Result 3: True

2. Equals(Object) Method

In ValueTuple<T1, T2, T3, T4, T5, T6>, the Equals(Object) method is used to return a value which shows that the current instance is equal to the specified object. In the Equals(Object) method, obj is considered to be equal to the current instance under the following conditions:

  • It is a ValueTuple value type.
  • The components are of the same types as that of the current instance.
  • The elements are equal to those of the current instance and the equality of each element is determined by the default equality comparer.

Syntax:

public override bool Equals (object obj);

Here, obj is the object to compare with this instance.

Return:The return type of this method is System.Boolean. It returns true if the given instance is equal to the specified object. Otherwise, return false.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// use of Equals(Object) method
using System;
  
namespace exampleofvaluetuple {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // 6-ValueTuple
        var u1 = ValueTuple.Create(4, 3, 5, 3, 9, 98);
        var u2 = ValueTuple.Create(4, 3, 5, 3, 9, 98);
  
        // Checking the given value
        // tuples are equal or not
        // Using Equal() Method
        if (u1.Equals(u2) == true) {
  
            Console.WriteLine("Both value tuples are equal");
        }
        else {
            Console.WriteLine("Both value tuples are not equal");
        }
    }
}
}

chevron_right


Output:

Both value tuples are equal


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.