Checking if two ValueTuple<T1,T2,T3,T4> 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>, 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> 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>) Method
- Equals(Object) Method
The Equals(ValueTuple<T1, T2, T3, T4>) method is used to check whether the two ValueTuple<T1, T2, T3, T4> 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>) 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> );
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 use of // Equals(ValueTuple<T1, T2, T3, T4>) // Method using System; namespace exampleofvaluetuple { class GFG { // Main Method static void Main( string [] args) { // 1-ValueTuple var u1 = ValueTuple.Create(47); var u2 = ValueTuple.Create(499); Console.WriteLine( "Result 1: {0}" , u1.Equals(u2)); // 2-ValueTuple var y1 = ValueTuple.Create(22, 33); var y2 = ValueTuple.Create(22, 444); Console.WriteLine( "Result 2: {0}" , y1.Equals(y2)); // 3-ValueTuple var a1 = ValueTuple.Create(23, 45, 67); var a2 = ValueTuple.Create(23, 45, 67); Console.WriteLine( "Result 3: {0}" , a1.Equals(a2)); // 4-ValueTuple var z1 = ValueTuple.Create(2, 37, 48, 1); var z2 = ValueTuple.Create(0, 23, 56, 78); Console.WriteLine( "Result 4: {0}" , z1.Equals(z2)); } } } |
Result 1: False Result 2: False Result 3: True Result 4: False
In ValueTuple<T1, T2, T3, T4>, 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: 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) { // 4-ValueTuple var u1 = ValueTuple.Create(4, 3, 5, 3); var u2 = ValueTuple.Create(4, 3, 5, 3); // 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" ); } } } } |
Both value tuples are equal
Contact Us