Compare two ValueTuple<T1, T2, T3, T4, T5, T6, T7> 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.
You can also compare the instance of two value tuples with each other by using CompareTo(ValueTuple<T1, T2, T3, T4, T5, T6, T7>) method. Or in other words, with the help of CompareTo method you are allowed to compare the current ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance to a specified ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance. This method also compares nested value tuples with each other.

Syntax:

public int CompareTo (ValueTuple<T1, T2, T3, T4, T5, T6, T7> other);

Return Type: The return type of this method is System.Int32. And it always returns a signed integer that indicates the relative position of this instance and other in the sort order as shown in the below list:



  • Negative integer for the instance precedes another.
  • Zero for the instance and others have the same position in the sort order.
  • Positive integer for the instance follows another.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// use of CompareTo method
using System;
  
namespace exampleofvaluetuple {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // 7-ValueTuple
        var t1 = (43, 34, 22, 7, 8, 29, 45);
        var t2 = (78, 98, 76, 67, 54, 78, 89);
        Console.WriteLine("Result 1: {0}", t1.CompareTo(t2));
  
        // 7-ValueTuple
        var p1 = (86, 99, 67, 88, 89, 96, 78);
        var p2 = (44, 22, 06, 55, 12, 16, 23);
        Console.WriteLine("Result 2: {0}", p1.CompareTo(p2));
  
        // 7-ValueTuple
        var q1 = (10, 20, 30, 40, 50, 60, 70);
        var q2 = (10, 20, 30, 40, 50, 60, 70);
        Console.WriteLine("Result 3: {0}", q1.CompareTo(q2));
    }
}
}

chevron_right


Output:

Result 1: -1
Result 2: 1
Result 3: 0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// use of CompareTo method
using System;
  
namespace exampleofvaluetuple {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // 7-ValueTuple
        var t1 = (44, 55, 66, 77, 88, 99, 33);
        var t2 = (44, 55, 66, 77, 88, 99, 33);
  
        // Using CompareTo Method
        if (t1.CompareTo(t2) == 0) {
  
            Console.WriteLine("Value tuples are equal!!");
        }
        else {
            Console.WriteLine("Value tuples are not equal!!");
        }
    }
}
}

chevron_right


Output:

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.