Expression-bodied members in C# 6

This is the sixth part of a series of posts I’m making into Upcoming Language Feature Changes in C# 6. (Now Visual Studio 2015 is available, they’re not so “upcoming”).

Back to the syntactic sugar updates this time, but again, I think this will be used a lot once it gets out in the wild.

There’s not a great deal to say as it’s basically lambda expressions in method and property declarations. In the following code, there’s a class called Rectangle that has several properties like xPos, yPos, Width, Height, etc

Method Declarations

The new syntax can be applied to both methods that return values:

// Before C# 6
public Rectangle Translate(int xTranslate, int yTranslate)
    return new Rectangle(xPos + xTranslate, yPos + yTranslate);

// With C# 6
public Rectangle TranslateNew(int xTranslate, int yTranslate) 
    => new Rectangle(xPos + xTranslate, yPos + yTranslate);

And to those that return void:

// Before C# 6
public void DisplayLocation()
    Console.WriteLine("{0}, {1}", xPos, yPos);

// With C# 6
public void DisplayLocationNew() => Console.WriteLine("{0}, {1}", xPos, yPos);

To be honest, it’s not a massive change, but should help reduce a lot of 1 line methods to something much more readable.

Property Declarations

You can also use the same lambda syntax

Using the same example, let’s imagine you’ve decided to declare Area as a getter only property:

// Before C# 6
public int Area {
    get { return Width* Height; }

// With C# 6
public int AreaNew => Width * Height;

As you can see, you don’t need to use the get keyword as it’s implicit.


This is another great feature to help increase productivity. Not only will the code you write be easier to read and understand, it will take less typing.

Leave a Reply

Your email address will not be published. Required fields are marked *