C# Expression-Bodied Members

Summary: in this tutorial, you’ll learn about expression-bodied members to make your code concise and more readable.

Introduction to expression-bodied members

C# introduced the expression body definition for methods and read-only properties in version 6 and expanded it to properties, constructors, finalizers, and indexers in version 7.

An expression body definition has the following syntax:

member => expression;Code language: C# (cs)

In this syntax:

  • The member can be a method, a property, a constructor, a finalizer, or an indexer.
  • The expression is any valid expression.

The purpose of the expression body definition is to make the code concise and more readable.

Methods

If a method has a single expression that returns a value whose type matches the method’s return type, you can use an expression body definition.

For example, the following example defines the Person class:

// Person.cs class Person { public string FirstName { get; set; } public string LastName { get; set; } public byte Age { get; set; } public string GetFullName() { return $"{FirstName} {LastName}"; } }Code language: C# (cs)

In this Person class, the GetFullName() method returns a string that matches the return type of the method:

public string GetFullName() { return $"{FirstName} {LastName}"; }Code language: C# (cs)

Therefore, you can use an expression-bodied method as follows:

public string GetFullName() => $"{FirstName} {LastName}";Code language: C# (cs)

And here’s the new Person class:

// Person.cs class Person { public string FirstName { get; set; } public string LastName { get; set; } public byte Age { get; set; } public string GetFullName() => $"{FirstName} {LastName}"; }Code language: C# (cs)

Read-only properties

Starting with C# 6, you can use expression body definition for a read-only property. For example, the following adds the CanVote read-only property to the Person class:

// Person.cs class Person { public string FirstName { get; set; } public string LastName { get; set; } public byte Age { get; set; } public bool CanVote { get { return Age >= 16 && Age <= 65; } } public string GetFullName() => $"{FirstName} {LastName}"; }Code language: C# (cs)

Since the CanVote property is read-only, you can use an expression body definition like this:

public bool CanVote => Age >= 16 && Age <= 65;Code language: C# (cs)

Here’s the updated Person class:

// Person.cs class Person { public string FirstName { get; set; } public string LastName { get; set; } public byte Age { get; set; } public bool CanVote => Age >= 16 && Age <= 65; public string GetFullName() => $"{FirstName} {LastName}"; }Code language: C# (cs)

Constructors

The following example defines the Skill class whose constructor has an expression that assigns the skill name to the Name property:

class Skill { public string Name { get; set; } public Skill(string name) { Name = name; } }Code language: C# (cs)

Hence, you can use the expression body definition for the Skill constructor as follows:

class Skill { public string Name { get; set; } public Skill(string name) => Name = name; }Code language: C# (cs)

Indexers

The following example defines the Matrix class that uses an index to access an element at a row and column:

// Matrix.cs class Matrix { private double[,] data; public Matrix(int row, int column) { data = new double[row, column]; } public double this[int row, int column] { get { return data[row, column]; } set { data[row, column] = value; } } }Code language: C# (cs)

In this example, the get and set accessors of the indexer have only one expression. So you can use an expression body definition as follows:

// Matrix.cs class Matrix { private double[,] data; public Matrix(int row, int column) => data = new double[row, column]; public double this[int row, int column] { get => data[row, column]; set => data[row, column] = value; } }Code language: C# (cs)

Summary

  • Use the C# expression-bodied members to make the code concise and more readable.
Was this tutorial helpful ?