Introduction
I am here to continue the series related to C# 7.0 features. Today, we will be going through existing and newly introduced expression-bodied members and will demonstrate their uses in an easy way.
Expression bodied members
Expression-bodied members are developed on top of anonymous types and lambda expressions and are not totally new to C# 7.0.
We already have methods and properties-based expression-bodied members in C# 6.0. However, several new members have been introduced in C# 7.0, as shown below.
- Expression-bodied constructor.
- Expression-bodied destructor.
- Expression-bodied getters.
- Expression-bodied setters.
Some of the characteristics of expression-bodied members are given below.
- They provide clean and crisp syntax.
- Expression-bodied members must have a name, return type, and returned expression.
Before jumping to the new members, let’s quickly review the existing ones.
Expression body methods & properties
Let’s look at the examples directly to understand how they can be used.
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Using expression bodied method:\n{new ExprBodiedMethodnProp().EmployeeDetail()}");
Console.WriteLine($"\nUsing expression bodied properties:\n{new ExprBodiedMethodnProp().EmployeeProp}");
}
}
class ExprBodiedMethodnProp
{
int empId { get; } = 101;
string empName { get; } = "Prakash";
string empAddress { get; } = "Kondapur, Hyderabad - 500084";
double empBasicSalary { get; } = 1000;
/// <summary>
/// Expression bodied method
/// </summary>
public double empHRA() => (empBasicSalary * 40) / 100;
/// <summary>
/// Expression bodied property
/// </summary>
public double empHRAProp => (empBasicSalary * 40) / 100;
/// <summary>
/// Expression bodied method
/// </summary>
public string EmployeeDetail() => $"EmpId: {empId}\nEmpName: {empName}\nEmpAddress: {empAddress}\nEmpHRA: {empHRA()}";
/// <summary>
/// Expression bodied property
/// </summary>
public string EmployeeProp => $"EmpId: {empId}\nEmpName: {empName}\nEmpAddress: {empAddress}\nEmpHRA: {empHRAProp}";
}
Output

As you can see in the above code, empHRA and EmployeeDetail methods are expression-bodied methods whereas empHRAProp and EmployeeProp are expression-bodied properties.
Generally, expression-bodied methods are more used than other members. They have the following characteristics.
- Expression-bodied methods can specify all the accessibility operators i.e. public, protected, internal, private, and protected internal.
- These can be declared virtual or abstract or can even override a base class method.
- Such methods can be static.
- Methods can even exhibit asynchronous behavior if they return void, Task, or Task<T>.
Expression body Constructors & Destructors
These are the new members introduced in C# 7.0. They allow an expression to be used in the constructor and destructor as well. The example given below explains the same.
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Using expression bodied constructor and destructor:\n{new ExprBodiedConsnDes().EmployeeDetail()}");
}
}
class ExprBodiedConsnDes
{
int empId { get; } = 101;
string empName { get; } = "Prakash";
string empAddress { get; } = "Kondapur, Hyderabad - 500084";
double empBasicSalary { get; }
/// <summary>
/// Expression bodied constructor
/// </summary>
public ExprBodiedConsnDes() => empBasicSalary = 1000;
/// <summary>
/// Expression bodied destructor
/// </summary>
~ExprBodiedConsnDes() => Console.WriteLine("\nI am a destructor of ExprBodiedConsnDes class");
public string EmployeeDetail() => $"EmpId: {empId}\nEmpName: {empName}\nEmpAddress: {empAddress}\nEmpBasicSalary: {empBasicSalary}";
}



Join the conversation! Your thoughts help the community grow.