Tuple equality – C# 7.3 in Rider and ReSharper

The Early Access Preview (EAP) versions of ReSharper 2018.2 and Rider 2018.2 added support for C# 7.3. In this series, we are looking at these new language features in C# 7.3. Today, we will cover tuple equality.

This post is part of a series:

Tuple equality

More language evolution can be found in support for tuples. In C# 7.0, C# 7.1 and C# 7.2, the concept of value tuples and deconstructing tuples was introduced and expanded upon. In essence, some syntactic candy that provides a memory-friendly and developer-friendly way of working with tuples.

C# 7.3 builds on top of this foundation and adds tuple equality (spec). The C# language now lets us compare equality of two tuples more easily. Instead of having to compare all of the tuple’s items one by one, we can check equality (using == or !=) on the tuple itself. Here’s a code snippet to help grok this:

This is pretty nifty compiler magic and syntactic sugar. Imagine comparing a tuple with 5 items, or a tuple with two items that are nullable…

It’s worth mentioning that tuple equality also works with tuple expressions, which can improve readability on some cases. The following statements are equivalent in C# 7.3:

What with nullable types? Before C# 7.3, we’d have to check all tuple items for null before comparing equality. With C# 7.3, we can instead check equality at the tuple level:

Under the hood, the C# compiler uses lowering to convert this language feature to what we’d normally have to write, as we can see in the generated Intermediate Language (IL) code (ReSharper | Tools | IL Code). The compiler converted our a == b check into many IL instructions that check whether our tuple’s nullable DateTime instances have a value.

IL code showing checks on HasValue etc.

Do check the specification for tuple equality, there are some interesting cases in there as well, for example when using nested tuples – think (string name, (string employee, DateTime employmentDate)).

Tuple equality definitely makes code more readable and concise!

Download ReSharper 2018.2 EAP now! Or give Rider 2018.2 EAP a try. We’d love to hear your feedback!

About Maarten Balliauw

Maarten Balliauw is a Developer Advocate at JetBrains, working on .NET tools and Space. He focuses on .NET, Azure, web technologies and application performance. Maarten is a frequent speaker at various national and international events. In his free time, he brews his own beer. Follow him on Twitter or check out his personal blog.
This entry was posted in How-To's and tagged , , , , , . Bookmark the permalink.

2 Responses to Tuple equality – C# 7.3 in Rider and ReSharper

  1. Pingback: Dew Drop - July 16, 2018 (#2766) - Morning Dew

  2. GreenMoose says:

    Great, next maybe let us define naming code styles for tuple members? :) ( https://youtrack.jetbrains.com/issue/RSRP-468289 )

Leave a Reply

Your email address will not be published. Required fields are marked *