A tuple provides a syntax that allows you to combine the assignment of multiple variables, of varying types, in a single statement. For more information, see the Tuples article.
==
) and inequality (!=
) operators.Let's consider the following simple example.
(int a, short b) tuple1 = (9, 13);
(long a, int b) tuple2 = (9, 13);
(long a, double b) tuple3 = (9, 13.0);
Console.WriteLine(tuple1 == tuple2); // output: True
Console.WriteLine(tuple1 != tuple2); // output: False
Console.WriteLine(tuple1 != tuple3); // output: False
Console.WriteLine(tuple3 == tuple2); // output: True
The equality (==
) and inequality (!=
) operations don't consider the tuple field names as shown below.
(int a, short b) tuple1 = (9, 13);
(long c, int d) tuple2 = (9, 13);
var tuple3 = (e: 9, f: 13.0);
Console.WriteLine(tuple1 == tuple2); // output: True
Console.WriteLine(tuple1 != tuple2); // output: False
Console.WriteLine(tuple1 != tuple3); // output: False
Console.WriteLine(tuple3 == tuple2); // output: True
You can compare two tuples with the following conditions.
==
) and inequality (!=
) operators. For example, (1, (2, 3)) == ((1, 2), 3)
doesn't compile because 1
is not comparable with (1, 2)
.The equality (==
) and inequality (!=
) operators compare tuples in a short-circuiting way. An operation stops as soon as it meets a pair of non-equal elements or reaches the ends of tuples. However, before any comparison, all tuple elements are evaluated as shown below.
int Print(int number)
{
Console.WriteLine(number);
return number;
}
Console.WriteLine((Print(10), Print(20)) == (Print(30), Print(40)));
You will see the following output when you execute the above example.
10
20
30
40
False