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>, 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> 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:
- Equals(ValueTuple<T1, T2, T3, T4, T5>) Method
- Equals(Object) Method
1. Equals(ValueTuple<T1, T2, T3, T4, T5>) Method
The Equals(ValueTuple<T1, T2, T3, T4, T5>) method is used to check whether the two ValueTuple<T1, T2, T3, T4, T5 > 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>) 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<T1, T2, T3, T4, T5>);
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:
using System;
namespace exampleofvaluetuple {
class GFG {
static void Main( string [] args)
{
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));
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));
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>, 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 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:
using System;
namespace exampleofvaluetuple {
class GFG {
static void Main( string [] args)
{
var u1 = ValueTuple.Create(4, 3, 5, 3, 9);
var u2 = ValueTuple.Create(4, 3, 5, 3, 9);
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
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!
Last Updated :
29 Dec, 2019
Like Article
Save Article