To compare two instances of ValueTuple you can use CompareTo method which is provided by ValueTuple structure. ValueTuple.CompareTo(ValueTuple) Method is used to compare the current instance of ValueTuple with another ValueTuple instance. It always returns zero if they are equal to each other.
Syntax:
public int CompareTo (ValueTuple other);
Here, other is the object to compare with the current instance.
Returns: The method always returns 0 of type System.Int32.
Exception: This method will throw an ArgumentException if the other is not ValueTuple instance.
Example 1:
using System;
class GFG {
static public void Main()
{
var MyTple1 = ValueTuple.Create(56, 45);
var MyTple2 = ValueTuple.Create(56, 3);
var MyTple3 = ValueTuple.Create(56, 45);
var MyTple4 = ValueTuple.Create(5345, 45);
int res1 = MyTple1.CompareTo(MyTple2);
int res2 = MyTple1.CompareTo(MyTple3);
int res3 = MyTple1.CompareTo(MyTple4);
Console.WriteLine( "Result 1: " + res1);
Console.WriteLine( "Result 2: " + res2);
Console.WriteLine( "Result 3: " + res3);
}
}
|
Output:
Result 1: 1
Result 2: 0
Result 3: -1
Example 2:
using System;
class GFG {
static public void Main()
{
var MyVTple1 = ValueTuple.Create(2018);
var MyVTple2 = ValueTuple.Create(2018);
if (MyVTple1.CompareTo(MyVTple2) == 0)
{
Console.WriteLine( "Welcome to GeeksforGeeks" );
}
else
{
Console.WriteLine( "Page Not Found" );
}
}
}
|
Output:
Welcome to GeeksforGeeks
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!