C# 6 Feature Preview: Expression Bodied Members

One of the new features in C# 6 is expression bodied members. This feature enables us to create members (either methods, property get accessors, or get methods for indexers) that are expressions rather than statement blocks.  Consider this class, and not the highlighted portion:

 

public class Point

{

    public double X { get; set; }

    public double Y { get; set; }

    public double Distance

    {

        get

        {

            return Math.Sqrt(X * X + Y * Y);

        }

    }

}

 

That’s quite a bit of typing for a very simple method.  C# 6 lets you replace that accessor with a lambda expression:

 

public double Distance => Math.Sqrt(X * X + Y * Y);

 

The body of the get accessor is replaced by the simple expression that returns the distance of the point from the origin.

As I mentioned above, expression bodied members can also be used for methods:

public override string ToString() => string.Format("{{0}, {1}}", X, Y);

 

Lest you worry that this feature only works with non-void methods, here’s a TranslateX method that moves the point in the X direction by x units. It has a void return:

public void TranslateX(double x) => X += x;

It’s one of the features I like a great deal in C# 6. When you use it with simple, one – line methods, it increases readability and clarity of a class.

When I speak on C# 6, I always get reservations from from people about how this feature could be abused. Developers are concerned that someone (not them mind you, but other developers they work with) would abuse this feature and write methods are are pages long using this syntax.

Don’t worry. The compiler won’t allow it.  Suppose I tried to extend the TranslateX method to take two parameters are translate the point in both the X and Y directions.  The compiler does not support expression bodied members with block statements.  None of these attempts to write a method with more than one statement as an expression bodied member will compile:

// Does not compile.

public void Translate(double x, double y) => X += x; Y += y;

 

// Does not compile.

public void Translate(double x, double y) => { X += x; Y += y; };

 

// Does not compile.

public void Translate(double x, double y) => if (true) { X += x; Y += y; };

 

At this point, even the most evil of your co-developers will give up and use the standard syntax for a method that has more than one statement.

Overall the new syntax leads to clearer code, and the restrictions on the syntax that the method must be an expression ensures that it can’t be abused (much).

Created: 12/4/2014 8:53:23 PM

Current Projects

I create content for .NET Core. My work appears in the .NET Core documentation site. I'm primarily responsible for the section that will help you learn C#.

All of these projects are Open Source (using the Creative Commons license for content, and the MIT license for code). If you would like to contribute, visit our GitHub Repository. Or, if you have questions, comments, or ideas for improvement, please create an issue for us.

I'm also the president of Humanitarian Toolbox. We build Open Source software that supports Humanitarian Disaster Relief efforts. We'd appreciate any help you can give to our projects. Look at our GitHub home page to see a list of our current projects. See what interests you, and dive in.

Or, if you have a group of volunteers, talk to us about hosting a codeathon event.