C# 7.0 and 7.1 support in ReSharper – Expression-bodied everything

Maarten Balliauw

ReSharper support for C# 7 and C# 7.1Today, we continue our blog series on ReSharper support for C# 7.0 and C# 7.1. The previous post touched on throw expressions (spec) and ReSharper’s inspections, quick-fixes and context actions around these language features.

Today, let’s talk about expression-bodied methods, properties, local functions, constructors, destructors and event accessors. Or in short: expression-bodied “everything”!

In this series:

Expression-bodied “everything”

In C# 6.0, expression-bodied methods and properties were introduced. In short, one-line methods or simple properties can be written with a simple lambda syntax, instead of having to write a full body:

// Before:
public string FullName
        return FirstName + " " + LastName;

// After:
public string FullName => FirstName + " " + LastName;

C# 7.0 extends expression-bodied members by allowing “everything” to be expression-bodied: methods, properties, local functions, but also constructors, destructors and event accessors. In the ReSharper settings under Code Editing | C# | Code Style, then Code Body, we can choose our preferred style (block body or expression body).
Code body code style

Once configured, ReSharper will suggest (or hint, warn or error, depending on our settings here) using a block body or expression body. A quick-fix can rewrite things for us.
Use expression body code analysis and quick-fix

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

Comments below can no longer be edited.

2 Responses to C# 7.0 and 7.1 support in ReSharper – Expression-bodied everything

  1. Marcel Popescu says:

    October 19, 2017

    Why is the clip showing Name still with the squiggles after refactoring?

    • Maarten Balliauw says:

      October 20, 2017

      Because of another inspection, to make it into a read-only property without backing field.


Subscribe to .NET Tools updates