As you know Microsoft has launched Visual Studio 2015 with .Net 4.6.  C# 6.0 has introduced with .Net 4.6 and there are lots of new features to add csharp 6.0 like Auto property and many more. Expression bodied member are one of them. You can make code more clean and readable to use this.  So, this post is basically for the new feature of C# 6.0 Expression bodied members.

Before C# 6.0, when we define any methods, properties, operators etc. then we need to provide curly  braces { } and return type, even if method contain one line in its body. But using Expression bodied member you can reduce code and make your code more readable and beautiful. Now you can use lambda expression instead of curly braces statement blocks.

Now in C# 6.0, you don’t need to write whole property or methods body you can just use lambda arrow => to return values. 

Example I
//Old Way

public string SiteName
{
      get
      {
         return "programmingwithmukesh.com";
      }
}

//New Way

public string SiteName => "programmingwithmukesh.com";

Here you can see how using lambda arrow you can make your code more simple and readable.

Example II
//Old Way

public int Add(int x, int y)
{
     return x+y;
}

//New Way

public int Add(int x, int y)=> x+y;

Here you can see for a one line method, if you want to return something in one line then it is very useful.

Example III
//Old Way

public void PrintName(string name)
{
     Console.WriteLine("Name is "+ name);           
}

//New Way

public void PrintName(string name) => Console.WriteLine("Name is "+name);
Conclusion:

So, Today we learned a new feature of C# 6.0 Expression Bodied Members with .Net 4.6, which help us to write code more clear.

I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks