In this article, I am going to share with you about Expression Bodied Members in C#
What is Expression Bodied Members?
- Expression Bodied member was introduced in C#6.0 and enhanced in C#7.0.
- Expression Bodied is a new technique or implementation to make code concise and readable.
- Expression Bodied makes the type member(Constructor, Destructor, Methods, Property, Indexer) defined in a single expression.
- Expression Bodied members can only be applied to the below list of type members:
- Constructor introduced in C#7.0
- Destructor introduced in C#7.0
- Property get accessor introduced in C#6.0
- Property set accfessor introduced in C#7.0
- Methods introduced in C#6.0
- Indexer introduced in C#7.0
The Syntax of expression body definition is,
member => expression;
where expression should be a valid expression and member can be any from above list of type members.
member => expression;
where expression should be a valid expression and member can be any from above list of type members.
Suppose we have a method or constructor that contains a single expression or line of code. Then In spite of using the traditional way to write code inside a method or constructor code block, we can write them as a single syntax. Those members who can follow this approach are known as Expression Bodied Members.
Constructor
Expression Bodied technique can be used to define a constructor in a single statement. This can only be achieved if a constructor has only a single valid expression. A valid expression can be either assignment or a method call. If a constructor has more than one line of code or expression then this technique cannot be applied.
- using System;
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Instance of Operations class created
- Operations ops = new Operations();
- Console.ReadLine();
- }
- }
- class Operations
- {
- public int Num1 = 100;
- public int Num2 = 33;
- // Constructor implemented using Expression Bodied
- // Expression or right side followed with => must be valid and should be one statement
- // Only assignment, call, increment, decrement, and new object expression can be used as a statement
- // This feature can only be used in C# 7.0 or onwards. Means In Visual Studio 2017 and onwards.
- // If we try to implement this feature in visual studio 2015 then the compiler will not allow doing so and shows an error.
- public Operations()=> Num1= Num1+Num2;
- }
- }
In the above program, Expression bodied is used with the constructor. Expression or right side followed with '=>' must be valid and should be one statement. Only assignment, call, increment, decrement, and new object expression can be used as a statement. A constructor will be called whenever an instance of 'Operations' class is created. A constructor does not have any return type and it is private by default. We have performed addition between two numbers and assign its value to "Num1".
Some other examples of valid syntax are,
- public Operation()=>Console.WriteLine("Value of Num1 is "+Num1+" and num2 is "+Num2);
- public Operation()=>Num1= Num1+(100*Num2)/2;
Some other examples of invalid syntax are,
//Incorrect as it contains two statements. Produce error as Only assignment, call, increment, decrement, and new object expression can be used as a statement
public Operation()=>Console.WriteLine("Value of Num1 is "+Num1+" and num2 is "+Num2); Num1=Num1+Num2;
public Operation()=>Console.WriteLine("Value of Num1 is "+Num1+" and num2 is "+Num2); Num1=Num1+Num2;
// Cannot declare a variable. Produce error as Only assignment, call, increment, decrement, and new object expression can be used as a statement
public Operation()=>int Num3= Num1+Num2;
public Operation()=>int Num3= Num1+Num2;
//Cannot declare a variable. Produces error as only assignment, call, increment, decrement, and new object expression can be used as a statement
public Operation()=>int i;
public Operation()=>int i;
Note
This feature is supported in C#7.0. So this feature can only be implemented in Visual Studio 2017 and onwards. If we try to implement this feature in visual studio 2015 then the compiler will not allow doing so and shows an error.
This feature is supported in C#7.0. So this feature can only be implemented in Visual Studio 2017 and onwards. If we try to implement this feature in visual studio 2015 then the compiler will not allow doing so and shows an error.
Destructor
Expression Bodied technique can be used to define Destructor in a single statement. A destructor is used to destruct the instances or release the resources used by the program and contain cleanup statements. This can only be achieved if Destructor has only a single valid expression. If Destructor has more than one line of code or expression then this technique cannot be applied.
- using System;
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Instance of Operations class created
- Operations ops = new Operations();
- Console.ReadLine();
- }
- }
- class Operations
- {
- public int Num1 = 100;
- public int Num2 = 33;
- // Constructor implemented using Expression Bodied
- // This feature can only be used in C# 7.0 or onwards. Means In visual Studio 2017 and onwards.
- public Operations()=> Num1= Num1+Num2;
- // Destructor implemented using Expression Bodied
- // Expression or right side followed with => must be valid and should be one statement
- // Only assignment, call, increment, decrement, and new object expression can be used as a statement
- // This feature can only be used in C# 7.0 or onwards. Means In visual Studio 2017 and onwards.
- // If we try to implement this feature in visual studio 2015 then compiler will will not allow to do so and shows an erro r.
- ~Operations()=> Console.WriteLine("Destructor is called");
- }
- }
In the above program, Expression bodied is used with destructor. Expression or right side followed with => must be valid and should be one statement. Only assignment, call, increment, decrement, and new object expression can be used as a statement. A destructor will be automatically called when the program exits. An instance is ready for destruction as soon as instance become useless. The destructor does not contain any return type or access modifier. We have written a line on the console.
Note
This Feature is supported in C#7.0. So this feature can only be implemented in Visual Studio 2017 and onwards. If we try to implement this feature in visual studio 2015 then the compiler will not allow doing so and shows an error.
This Feature is supported in C#7.0. So this feature can only be implemented in Visual Studio 2017 and onwards. If we try to implement this feature in visual studio 2015 then the compiler will not allow doing so and shows an error.

Sanwar RanwaPosted Feb 2, 2018, 10:53 PM
Good share ......................
Hiten PandyaPosted Jan 19, 2018, 7:12 AM
Good Share with simple examples.. Thanks....
Sagar Pandurang KapPosted Jan 19, 2018, 7:00 AM
Good one.Well described..
Naresh SinghalPosted Jan 18, 2018, 11:34 PM
Very well explained with neat example....