Open In App

Checking if two ValueTuple<T1,T2,T3,T4,T5,T6,T7> 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, T7>, 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, T7> 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, T7>) Method
  2. Equals(Object) Method

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

The Equals(ValueTuple<T1, T2, T3, T4, T5, T6, T7>) method is used to check whether the two ValueTuple<T1, T2, T3, T4, T5, T6, T7 > 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, T7 >) method the other parameter is considered to be equal to the current instance under the following conditions:

Syntax:



public bool Equals (ValueTuple<T1, T2, T3, T4, T5, T6, T7>);

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:




// C# program to illustrate the 
// Equals(ValueTuple<T1, T2, T3,
// T4, T5, T6, T7>) 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));
    }
}
}

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

2. Equals(Object) Method

In ValueTuple<T1, T2, T3, T4, T5, T6, T7>, 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:

Syntax:

public override bool Equals (object obj);

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

Return Type: 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:




// C# program to illustrate the 
// use of Equals(Object) method
using System;
  
namespace exampleofvaluetuple {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // 7-ValueTuple
        var u1 = ValueTuple.Create(4, 3, 5, 3, 9, 98, 1);
        var u2 = ValueTuple.Create(4, 3, 5, 3, 9, 98, 1);
  
        // 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");
        }
    }
}
}

Output:
Both value tuples are equal

Article Tags :
C#