Today, 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 (we will update the table of contents as we progress):
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:
public string FullName
return FirstName + " " + LastName;
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).
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.