How to compare two tuples in C#?


Tuple comparison came after C# 7.3.

Easily compare two tuples using the equality operator in C#.

Let’s say we have two tuples −

var one = (x: 1, y: 2);
var two = (p: 1, 2: 3, r: 3, s:4);

To compare them, just use the == operator −

if (one == two)
Console.WriteLine("Both the tuples are same (values are same).");

Let use see the code −

Example

var one = (x: 1, y: 2);
var two = (p: 1, 2: 3, r: 3, s:4);

if (one == two)
Console.WriteLine("Both the tuples are same (values are same).");
lse
Console.WriteLine("Both the tuples values are not same.");

Updated on: 21-Jun-2020

296 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements