15 Apr 2015
C# 6
Want to learn about other C# 6 features? Check out the full list of articles & source code on GitHub

Expression bodied members, which is a new feature in C# 6 and is a very interesting feature which aims to have less lines of code in your app for simple things. What do I mean by simple things? Anything which is ONE statement.

I use the word statement and not line since you can break a statement across multiple lines and it will work. Think of statement to mean, when you hit your first ; you are done.

Here are some examples of one statement code blocks:

// a method with one statement in it
private double Tax()
{
    return 1.14;
}

// a read-only property with one statement
public double Price
{
    get
    {
        return CostPrice * Tax();
    }
}

// a method calling an event, in this case using the nullet operator
private void RaisePain(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

All examples have one one statement and all examples are also multiple lines of code. The first and last examples are 4 lines of code (4 to 1 ratio) and the second example is 7 lines of code (7 to 1 ratio). Expression Bodied Members aims to get that to a 1 to 1 ratio! Smile

Expression Bodied Members

The name of this feature hints at everything you need to know for this: This is for members (properties, methods etc…), this is an expression and so we will use the => operator and that operator will be followed by the body of the expression. If we apply that logic to the first example we can rewrite them as this:

// still a method, just one line
private double Tax() => 1.14;

// still a readonly property
public double Price => CostPrice * Tax();

// still a method
private void RaisePain(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

This really cuts the code to statement ratio WAY down and lets you express simple things without much ceremony.

While it uses the => operator, I have intentionally NOT called these lambda’s because they do not support all the functionality of a lambdas.

Supported and Not Supported Scenarios

As we have seen both methods, with or without parameters, and read-only properties (i.e. a property without a setter) are supported. Operators are also supported:

public static Complex operator +(Complex a, Complex b) => a.Add(b);

Converters (implicit or explicit) are also supported:

public static implicit operator string(Name n) => n.First + " " + n.Last;

Indexers are also supported:

public Customer this[Id id] => store.LookupCustomer(id);

There are also members not supported by this:

  • Constructors: Often they have side effects and do not return anything plus there are a lot of complexity in them (inheritance for example) which makes them a poor fit.
  • Events: They need a add & remove code, so they can’t fit the one statement thinking.
  • Finalisers: Same issues as constructors

In summary, I think think that this should provide for a cleaner/simpler code but let me know what you think about it?

Tags: 

Add new comment