Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Compare Tuples In C#
WhatsApp
Mahesh Chand
6y
57.2k
0
14
100
Article
Tuples are useful in C# language when it comes to returning and working with a group of items. If you’re new to tuples, check out
Tuples in C#
article to learn more about tuples.
You can compare two tuples by using equality == and != operators. The process compares left side of the tuple members with the right side of the tuple members in order. The == operator stops evaluating once it finds that member of the left side does not match with the member on the right side.
Tuple comparison is available in C# 7.3 or later versions. If you try to use it in previous versions, you will see the following error message.
In case of == operator, if the left side member value does not match with the right side member value, it will stop comparing and return false. The following code snippet compares two tuples and checks if the values are equal.
var tupleA = (one: 1, three: 3, five: 5);
var tupleB = (a: 1, b: 3, c: 5);
if
(tupleA == tupleB)
Console.WriteLine(
"Tuples values are same."
);
else
Console.WriteLine(
"Tuples values are different."
);
The != operator stops evaluating members as soon as one pair is equal. The following code snippet uses != operator to check if values are different.
if
(tupleA != tupleB)
Console.WriteLine(
"Tuples values are different."
);
else
Console.WriteLine(
"Tuples values are same."
);
Tuples support implicit conversions. Means, you can compare an int value tuple with a long value tuple.
Next >
Tuples in C#
Compare Tuples
Tuple
Tuples
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.
Membership not found