C# 6 Feature Review: Expression-Bodied Function Members

In the last post, I looked at auto-property enhancements, with several comments pointing out some nicer usages. I recently went through the HtmlTags codebase, C# 6-ifying “all the things”, and auto property and expression bodied function members were used pretty much everywhere. This is a large result of the codebase being quite tightly defined, with small objects and methods doing one thing well.

Expression-bodied function members can work for both methods and properties. If you have a method with one statement, and that statement can be represented as an expression:

Or a getter-only properties/indexers with a body that can be represented as an expression:

You can collapse those down into something that looks like a cross between a member declaration and a lambda expression:

This seems to work really well when the expression can fit neatly on one line. In my refactorings, I did have places where it didn’t look so hot:


If the original expression looked better on multiple lines formatted out, the new expression-bodied way will look a bit weirder as you don’t have that initial curly brace. Refactoring tools try to put everything on the one line, making it pretty difficult to understand what’s going on. However, some code I have refactored down very nicely:

It was a bit of work to go through the entire codebase, so I wouldn’t recommend that approach for actual production apps. However, it’s worth it if you’re already looking at some code and want to clean it up.

Verdict: Useful often; refactor code as needed going forward

C# 6 Feature Review: Auto-Property Enhancements