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:

public string GetFullName() {
  return FirstName + " " + LastName;
}

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

public string FormalName {
  get { return FirstName[0] + ". " + LastName; }
}

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

public string GetFullName() => FirstName + " " + LastName;

public string FormalName => FirstName[0] + ". " + LastName;

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:

public Accessor Prepend(PropertyInfo property)
{
  return
    new PropertyChain(new IValueGetter[] {
     new PropertyValueGetter(property), 
     new PropertyValueGetter(InnerProperty)});
}

After:

public Accessor Prepend(PropertyInfo property) => 
    new PropertyChain(new IValueGetter[] {
     new PropertyValueGetter(property), 
     new PropertyValueGetter(InnerProperty)});

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:

public class ArrayIndexer : Accessor
{
  private readonly IndexerValueGetter _getter;

  public ArrayIndexer(IndexerValueGetter getter)
  {
    _getter = getter;
  }

  public string FieldName => _getter.Name;
  public Type PropertyType => _getter.ValueType;
  public PropertyInfo InnerProperty => null;
  public Type DeclaringType => _getter.DeclaringType;
  public string Name => _getter.Name;
  public Type OwnerType => DeclaringType;

  public void SetValue(object target, object propertyValue) => _getter.SetValue(target, propertyValue);

  public object GetValue(object target) => _getter.GetValue(target);

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

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in C#. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • AlexanderTank

    With expression bodies my ASP.NET MVC controller actions became a one liner:
    public async Task UpdateUser(UpdateUser.RequestCommand cmd) => await ExecuteCommandAsync(cmd);

  • after 15 of years with curly braces in c, c++, javascript, go and c# it looks weird without!

    • Nathan Armstrong

      Embrace the reduced cruft! Start training your eyes to look past the curly braces and other similar ‘pomp and ceremony’ syntax. Rough, but rewarding. :)

  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1993()

  • Jeckel

    Recently I’ve been running across this syntactic sugar more and more and while I don’t like it and won’t be using these operators myself, this article was very helpful in understanding what they are and what they do. Thank you for the explanation.