Enchanced Expression bodied members in C# 7.0

In C#6.0 there was introduced a new syntactic feature which is Expression bodied members. Lets say you have functions sometimes which are not doing much then just returning a single value or just doing a simple one line task. And to have this method you have to go through the whole method body syntax. post10_ex1

The above code is just doing a single line task and for that he is maintaining a method body. So C#6.0 introduced new feature called Expression Bodied Members which is actually inspired from Lambda expressions. But also has name of the expression.

post10_ex2

This line is doing just the same job but with a brilliantly improved syntax. Now this method can be called as any other method.

In C#6.0 this feature was provided for properties of the class as well.

post10_ex4

Though it was an awesome feature but it was limited to just methods and properties in C#6.0. But now in C#7.0 Expression Bodied Members are also supported with Constructors, Destructors, Accessors and Finalizers.

post10_ex5

You can see that in the code above that how it is improving the syntax for get and set accessors of the Name property. Also Constructor and Destructor of the class are making it more elegant. And please don’t miss the last line of code for the improved way of throwing the exceptions.

I hope you enjoyed this small bit of information. That’s all for today. See you tomorrow 🙂

  • Show Comments (0)

Your email address will not be published. Required fields are marked *

comment *

  • name *

  • email *

  • website *