Embrace deconstruction with ReSharper 2018.1

Deconstruction is a great feature that was introduced with C# 7. A while ago, we showed how ReSharper (and Rider) can help generate deconstructors for us. ReSharper 2018.1 adds even more enhanced support to fully embrace the elegance and power of deconstructions with new code styles, quick-fixes, context actions, better refactoring support and more!

In the next two posts, we’ll go over these new features. This first post is dedicated to new code styles. The second part will cover everything else. Let’s dive in!

New code styles

Consistent deconstructions

Deconstruction is a way of a decomposing tuple (or any other value whose type has a deconstructor) into parts. The decomposed parts can be assigned either to fresh variables — deconstructing declaration or to existing variables – deconstructing assignment.

Deconstructing declaration, unlike assignment, can be expressed using two slightly different syntaxes.

The first, more concise, syntax puts the var outside of the parentheses and tells the compiler to infer types of every deconstructed variable:

The second syntax uses separate declarations for each deconstructed variable and allows either to specify variable type explicitly or use var:

The nice thing about deconstructions is that they can be nested, i.e. whenever a decomposed part is a tuple itself (or of type with custom deconstructor), we can deconstruct it right within outer deconstructing declaration.

Not every deconstruction syntax can be used together, namely if we put var outside of parentheses, then neither explicit types nor vars should be used inside:

In order to make deconstructing declarations consistent across the code base, ReSharper 2018.1 introduces the new code style setting Prefer separate declarations for deconstructed variables. Note that plain old ‘var’ Usage in Declarations code style is applied only when separate declarations are preferred.

Once configured, ReSharper highlights deconstructing declarations that are not following the code style, and suggests quick-fixes Use single deconstruction declaration or Use separate declaration expressions. Needless to say that quick-fixes can also be applied in scope of file, current project or the whole solution. And as always, ReSharper provides accompanying context actions which allow us to switch between alternative syntaxes.

Deconstruction code style demo

Consistent discards

Sometimes, when deconstructing a value, we only care about a subset of its components. In such cases, irrelevant components can be ignored using the discards feature of C#, expressed as _ (a single underscore):

Because C# 7 allows us to optionally omit the type of discarded variables, ReSharper 2018.1 adds one more code style setting – Use ‘var’ keyword for discards – to configure whether var _ is preferred over _. The same code style is also applied to discards used as out parameters. For any inconsistencies found, ReSharper suggests to apply quick-fixes Remove redundant ‘var’ keyword or Use explicit discard declarations.

Deconstruction code style demo

Stay tuned for our next post, where we explore more possibilities around deconstruction!

Download ReSharper Ultimate 2018.1 or check out Rider 2018.1. We’d love to hear what you think of these new features.

This entry was posted in How-To's and tagged , , , , . Bookmark the permalink.

Leave a Reply

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