Check if ValueTuple instances are equal in C#

ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other.
In value tuples, you can check if two value tuples are identical or not by using the ValueTuple.Equals Method. This method will return value which indicates whether the given ValueTuple instance is equal to the specified object or not. It will return true if the given tuples are equal, otherwise, return false. This method can be overloaded in two different ways:

  1. Equals(ValueTuple) Method
  2. Equals(Object) Method

Equals(ValueTuple) Method

The Equals(ValueTuple) method is used to check whether the two ValueTuple instances are equal. It always returns true. The return type of this method is System.Boolean.

Syntax:



public bool Equals (ValueTuple other);

Here, other is a value tuple to compare with the current instance.

Returns: It always returns true.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate to check the
// given ValueTuples are equal or not
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating a value tuples with two elements
        var MyTple1 = ValueTuple.Create(56, 45);
        var MyTple2 = ValueTuple.Create(56, 45);
  
        bool res1 = MyTple1.Equals(MyTple2);
        Console.WriteLine("Is MyTple1 and MyTple2 equal?: " + res1);
    }
}

chevron_right


Output:

Is MyTple1 and MyTple2 equal?: True

Equals(Object) Method

The Equals(Object) method is used to return a value that determines whether the current ValueTuple instance is equal to a specified object. This method will return true if the given ValueTuple instance is equal to a specified object, otherwise, it will return false.

Syntax:

public override bool Equals (object obj);

Here, obj is the object to compare to the current instance.

Return Type: The return type of this method is System.Boolean.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to check the 
// given value tuples is equal or not 
// using Equal(Object) method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating a value tuples 
        // with one element
        var MyTple1 = (56);
          
        object ob1 = 56;
  
        // Check the given object is equal or not
        // Using Equals(Object) method
        bool res = MyTple1.Equals(ob1);
        if (res == true
        {
            Console.WriteLine("The given object is equal to"+
                            " the value tuple element...!!");
        }
  
        else
        {
            Console.WriteLine("Not equal....!!");
        }
    }
}

chevron_right


Output:

The given object is equal to the value tuple element...!!

Reference:



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.