Expression Bodied Members In C#


The Expression-bodied member feature introduced in C# 6. It is enhanced in C# 7 to allow expression bodied member with constructor and destructor. Expression-bodied means method or property body write as an expression. We can use expression body in following cases.

  1. When a method returns single value or void.
  2. The method has a single expression as a method body.
  3. Read Only Properties and Properties with accessors which have single expression.
  4. Constructor and finalize with a single expression.
You may like to read

  1. Ref Return and Ref Local In C#
  2. Pattern Matching In C#

Expression-Bodied Method


It is introduced in C# 6. It uses in those methods which have a single line of code, and that statement can be represented as an expression. It removes both curly braces and the return keyword from method. The right-hand side of Lambda arrow (=>) represents expression which executes. Let’s see an example.

Expression-Bodied Property


This feature is also introduced in C# 6. One line properties with only get accessor can be written with the lambda syntax as an expression. Let’s see an example.

We create a class named Rectangle. It has properties to calculate Area (old way) and Perimeter (new way) as per following code snippet.

This way reduces code and don’t write get accessor for property. Now, use it in execute program as per following code snippet.

Expression-Bodied Property Accessors


This feature introduced in the C# 7 language. When properties accessors have a single line of code which can be represented as an expression then we use expression-bodied for same.As it just a single line code, so it simplified the code. This feature is also available with C# 6 but only for get accessor i.e for read-only property. Let’s see an example

We create a class named Person.It has two properties one is Age and another is IsAdult. The Age property uses expression for both set and get accessor while IsAdult uses expression for get accessor only. The following code snippet for same.

Now, we use these properties of Person class in the entry class as per following code snippet.

Expression Bodied Constructor and Destructor


Both are introduced in the C# 7.0. When these have a single line of code which can be represented as an expression then we use expression-bodied constructor and destructor accordingly. Let’s see an example.

We create a class named Employee. Constructor and destructor have the same name as class name. We create constructor and destructor in this class as per following code snippet.

This example does not need a destructor, but it is shown to demonstrate the syntax. We should not implement a destructor in our class unless it is necessary to release unmanaged resources such as file operations.

Now, we use the constructor of Employee class in the entry class as per following code snippet.

Conclusion


We have gone through most of expression bodied members. Some members are existing with C# 6.0 and some introduced in C# 7.0.