Jump to content
Aerosol

What’s new in C# 6.0? - Expression-bodied function

Recommended Posts

Posted

Did you explore the latest of C# 6.0? It brought another new feature called “Expression-bodied function members” which will allow you to write expression bodies for properties, methods etc. instead of writing statement blocks. Thus reducing the code. Today in this post, we will discuss about the new

Did you explore the latest of C# 6.0? It brought another new feature called “Expression-bodied function members” which will allow you to write expression bodies for properties, methods etc. instead of writing statement blocks. Thus reducing the code.

Today in this post, we will discuss about the new feature “Expression-bodied function members”. Read more to learn about it. Don’t forget to share the feature links in your network.

Whats%252520new%252520in%252520CSharp%2525206.0%252520-%252520header%25255B6%25255D.png?imgmax=800

Don’t forget to read my previous posts on this series:

C# 6.0 brought another great new feature named “Expression-bodied function members” along with Visual Studio 2015 and .NET 4.6. If you didn’t try yet the preview version of the new IDE, go and grab it to get your hands dirty with the new features before it actually releases.

Expression-bodied function members allow properties, methods, operators and other function members to have bodies as lambda like expressions instead of statement blocks. Thus reducing lines of codes and clear view of the expressions.

Now in C# 6.0, instead of writing the whole property body using getters/setters, you can now just use the lambda arrow (“=>”) to return values. For example, the below code returns the string “Kunal Chowdhury” when you access the property “New Name”. Remember that, in this case you don’t have to write the “return” keyword. The lambda arrow (=>) will do the same for you internally.

1%25255B2%25255D.png?imgmax=800

Similar to properties, you can also write expressions for methods/functions which returns value to the caller. For example, the below code will return sub of the parameters when you call the “NewSum” method.

2%25255B2%25255D.png?imgmax=800

Here is another example to show you how you can use the expression-bodied function members while declaring the methods. Please note that the effect is exactly the same as if the methods had a block body with a single return statement.

3%25255B2%25255D.png?imgmax=800

Not only returning methods, you can use the feature in void returning methods, as well as Task returning async methods too. The lambda arrow syntax (“=>”) still applies but the expression following the arrow must be a statement expression (just as is the rule for lambdas):

4%25255B2%25255D.png?imgmax=800

Though it is still not much clear on what additional benefit it will provide to us, but definitely it will reduce some codes, brackets etc. and give us a clean code. What additional benefits you think? Drop a line and share with us. Whatever the case, the new features are really very exciting. Isn’t it? Which feature you liked the most?

Don’t forget to subscribe to my blog’s RSS feed and Email Newsletter to get the immediate update directly delivered to your inbox. I am available on Twitter, Facebook, Google + and LinkedIn. Do connect with me on those social networking sites and get the updates which I share.

Reference: Kunal-Chowdhury.com

Source

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...