C# 7.0 and 7.1 support in ReSharper – Out variables

ReSharper support for C# 7 and C# 7.1Back in February, we wrote a State of the union about ReSharper C# 7 support, based on ReSharper 2016.3. Since then, a number of improvements have been made, both to C# as well as ReSharper (and Rider, which gets language support updates from ReSharper for free).

ReSharper 2017.1 and 2017.2 both bring better support for C# 7.0 and C# 7.1, with a number of new inspections, quick-fixes and context actions. ReSharper understands the new syntax and constructs that C# 7.0 and C# 7.1 bring, and ties them into existing and new inspections, quick-fixes, navigation and refactorings:

  • Binary literals (spec) and digit separators (spec) – see here for ReSharper features (C# 7.0)
  • Local functions (spec) – see here for ReSharper features (C# 7.0)
  • out variable declarations (spec) (C# 7.0)
  • throw expressions (spec) (C# 7.0)
  • Expression-bodied “everything” (C# 7.0)
  • async main methods (spec) (C# 7.1)
  • default literal (spec) (C# 7.1)
  • Inferring of tuple names (spec) (C# 7.1)
  • Pattern matching with generics (spec) (C# 7.1)

Let’s start a blog series on how these language features translate to ReSharper and Rider (we will update the table of contents as we progress):

  • Out variables

We’re kicking off this series with out variable declarations (spec) that are available in C# 7.0.

Out variables

In earlier C# versions, using out parameters would mean declaring a variable with its full type and then making a method call:

With C# 7.0, we can instead make a method call and declare our variable in one go, using out variable declarations:

We can use the Inline ‘out’ variable declaration inspection (and quick-fix) to inline the variable declaration in our method call.

Inline out variable

Another inspection will check our preferred code style and suggests using var (or the use of explicit types if configured):

Use var inspection and quick-fix to convert between var and specific type

C# 7.0 also introduces ref returns, where a method can return a reference to a variable. ReSharper provides an inspection to verify method signatures using ref returns are valid, and comes with a quick-fix to return by reference or update the base type and make it return by value.

An inspection ensures return by ref is used correctly

Download ReSharper Ultimate or check out Rider that takes advantage of ReSharper’s language support updates. We’d love to hear your thoughts.

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

3 Responses to C# 7.0 and 7.1 support in ReSharper – Out variables

  1. Rich K says:

    A number of links to the C# specs in GitHub are 404s

  2. Dev says:

    I find it really inconvenient that it goes to a type rather than ‘out var’ by default. So always have to repeat to commands. Also annoying that new classes are not sealed by default. Could you create two broad default non-granular R# profiles: “I am a legacy developer who loves imperative stuff” and “I am a modern developer who embraces immutability and inference”?

Leave a Reply

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