Dotnet logo

.NET Tools

Essential productivity kit for .NET and game developers


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! In this series:

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:

int number;
if (int.TryParse("42", out number)) {
  // ... work with number ...

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

if (int.TryParse("42", out int number)) {
  // ... work with number ...

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.

image description

Discover more