Today we are going to explore yet another new feature of C# 6.0 which will be coming in this release. This is my fifth post in the series of C# 6.0 New Features. Don't forget to read my previous post in this Series :
In C# 6.0 Microsoft has included yet another great feature called as "Expression-bodied Function" and to explore and experience more about that you need to download Visual Studio 2015 and .NET Framework 4.6.
Expression-bodied function allows properties, methods, operators and other functions to have bodies just like Lambda expressions instead of blocks of statements, thus reducing lines of codes and becomes more readability. But from my experience there are many Developers who still prefers writing code in the old fashion because according to them it’s clearer and less confusing. Anyways it all comes to the personal choice.
So now in C# 6.0 instead of writing the whole property body using getters and setters, Developer can now just use the lambda operator (=>) to return values. Below is the example.
And very similar to property, you can use Expression Bodied function to methods which returns value to the caller. Below is the example.
Developer can use this feature even for methods returning void as well as Task returning async. Syntax remains the same as above example.
How exactly it will be beneficial is still a question but it will definitely reduce the line of codes, which I am sure some would prefer and some would not.