What's New in C# - Pattern Matching and Local Functions

What’s New in C# 7.0? – Expression-Bodied Members and Throw Expressions

C# 7.0, the latest major version of the extremely popular programming language, was released in March 2017 alongside Visual Studio 2017, bringing a number of new features and capabilities to the table. Today we’ll continue our deep dive into some of these awesome features in our ongoing series, What’s New in C# 7.0?:

  • In Part 1 we thoroughly explored tuple types, tuple literals, and out variables.
  • In Part 2 we looked at pattern matching and local functions.
  • In Part 3 we examined the digit separator, binary literals, returning references and local reference variables.

Today in part 4 we’ll cover new expression-bodied members and throw expressions, so let’s get going!

New Expression-bodied Members

C# 6.0 introduced expression body definitions with method and property get declarations. The expression body definition syntax allows single-expression bodies to be written in a more concise and compact format. At the most basic level, an expression-bodied member looks like: member => expression;.

As of C# 6.0, expression bodies can be used for methods and property get declarations. For example, here is a basic User class that provides the Email and Name properties using expression body syntax. The same syntax is also used for the ToString() override method:

C# 7.0 adds a number of new expression-bodied members to the list valid list, including constructors, finalizers, property setters, and indexers. For the first three, below we’ve modified our User class to include all expression-bodied members (except an indexer, since it doesn’t make much sense in this context):

We also have a UserCollection class that contains a private User[] _users property:

We can then instantiate a new UserCollection instance, use the indexer to retrieve the second User element, and then output the returned value to the console (using the ToString() override method we defined earlier):

Throw Expressions

Closely related to expression-bodied members, C# 7.0 also introduces the ability to create throw expressions. In many places where an expression might be valid, a throw expression can be used to directly throw an Exception. For example, here we’re causing attempts to use the setter of the User.Email property to throw a new NotImplementedException:

The Sharpbrake library provides robust exception tracking capabilities for all of your C# and .NET applications. Sharpbrake provides real-time error monitoring and automatic exception reporting across your entire project, so you and your team are immediately alerted to even the smallest hiccup, and can appropriately respond before major problems arise. With a robust API and tight integration with the powerful Airbrake web dashboard, Sharpbrake will revolutionize how your team manages exceptions.

Check out all the great features Sharpbrake brings to the table and see why so many of the world’s top development teams use Airbrake to dramatically improve their exception handling practices!