C# 7.0 Expression Bodied Members

C# 6 introduced expression bodied members with methods and properties. This feature has been enhanced with C# 7.0 to allow expression bodied members with constructors, destructors, and also property accessors. This article gives a review on expression bodied members with C# 6, and shows the new options with C# 7.0.

LANCIA Lambda Spider Corsa MM 1928

Expression-Bodied Methods (C# 6)

With expression-bodied methods, a method that includes just one statement can be written with the lambda syntax:

C# 5.0

public bool IsSquare(Rectangle rect)
{
    return rect.Height == rect.Width;
}

C# 6

public bool IsSquare(Rectangle rect) => rect.Height == rect.Width;

This removes not only the curly brackets, but also the return keyword. Return is implicit with a lambda expression.

Lambda expressions can be written in short form without curly brackets when the statement consists of a single line. Lambda expressions can also be written in the long form where curly brackets and the return statement are needed. This longer syntax is not possible with expression-bodied members. If one code line is not enough, you can use the normal syntax with curly brackets, as is available since C# 1.0.

Expression-Bodied Properties (C# 6)

Similar to expression-bodied methods, one-line properties with only a get accessor can be written with the same lambda syntax.

C# 5.0

public string FullName
{
    get
    {
        return FirstName + " " + LastName;
    }
}

C# 6

public string FullName => FirstName + " " + LastName;

This syntax reduces the code not only by removing curly brackets, but also writing the get accessor is not necessary. The code that’s generated from the compiler is the same.

Expression-Bodied Constructors (C# 7.0)

After we’re used to the shorter syntax, expression-bodied members can now be written with constructors as well. In case the constructor just needs one line, the code can be simplified:

C# 6

public Resource()
{
    Console.WriteLine($"ctor {nameof(Resource}");
}

C# 7.0

public Resource() => Console.WriteLine($"ctor {nameof(Resource}");

Expression-Bodied Destructors (C# 7.0)

The same is true for destructors:

C# 6

public ~Resource()
{
    Console.WriteLine($"destructor {nameof(Resource}");
}

C# 7.0

public ~Resource() => Console.WriteLine($"destructor {nameof(Resource}");

Expression-Bodied Property Accessors (C# 7.0)

With the new options for expression-bodied members, I see the biggest advantage in expression-bodied property accessors. With accessors that consist of just one line, the code can be simplified:

C# 6

private int _x;
public X
{
    get 
    { 
        return _x; 
    }
    set 
    { 
        _x = value; 
    }
}

C# 7.0

private int _x;
public X
{
    get => _x;
    set => _x = value;
}

Of course in such a simple scenario, I would use an auto property (available since C# 2.0), which reduces the code more:

public int X { get; set; }

However, I often need properties with a different implementation, e.g. using a INotifyPropertyChanged implementation from a base class. Here, get and set accessors are needed:

C# 6

private int _x;
public X
{
    get 
    { 
        return _x; 
    }
    set 
    { 
        SetProperty(ref _x, value); 
    }
}

This is now a useful example of the shorthand notation:

C# 7.0

private int _x;
public X
{
    get => _x;
    set => SetProperty(ref _x, value);
}

Expression-Bodied Event Accessors

Of course expression-bodies also work with events. Instead of writing

C# 6

private EventHandler _someEvent;
public event EventHandler SomeEvent
{
    add
    {
        _someEvent += value;
    }
    remove
    {
        _someEvent -= value;
    }
}

You can now write:

C# 7.0

private EventHandler _someEvent;
public event EventHandler SomeEvent
{
    add => _someEvent += value;
    remove => _someEvent -= value;
}

Of course in such a simple scenario you can also use the shorthand notation as follows. However, in scenarios where you need to do more (described in my book Professioinal C# 6), the new syntax can be helpful.

C#

public event EventHandler SomeEvent;

Summary

C# 7.0 continues with productivity enhancements. Expression-bodied members have been available with C# 6 for methods and properties, now they can be used with constructors, destructors, property accessors, and event accessors as well.

Related

Other C# 7.0 features I’ve already written about:

Binary Literals and Digit Separators

Tuples

Sample Code

The sample code is available at GitHub.

To run the sample code you need Visual Studio 2017 RC.

Have fun with programming and learning!
Christian

More Information

Update: more information on C# 7 features in my new book Professional C# 7 and .NET Core 2.0 with source code updates at GitHub.

More information about C# and expression-bodied members is available in my new book and my C# workshops:

Professional C# 6 and .NET Core 1.0

Christian Nagel’s Workshops

Image from © Massimocampanari | Dreamstime.com LANCIA Lambda Spider Corsa MM 1928

Advertisement

7 thoughts on “C# 7.0 Expression Bodied Members

Leave a Reply to Christian Nagel Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.