Click here to Skip to main content
13,769,067 members
Click here to Skip to main content
Add your own
alternative version

Stats

28.3K views
8 bookmarked
Posted 27 Dec 2014
Licenced CPOL

What’s New in C# 6.0? - Expression-bodied Function

, 27 Dec 2014
Rate this:
Please Sign up or sign in to vote.
What's new in C# 6.0? - Expression bodied function

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.

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.

What’s new in C# 6.0? - Expression-bodied function (www.kunal-chowdhury.com)

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.

What’s new in C# 6.0? - Expression-bodied function (www.kunal-chowdhury.com)

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.

What’s new in C# 6.0? - Expression-bodied function (www.kunal-chowdhury.com)

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):

What’s new in C# 6.0? - Expression-bodied function (www.kunal-chowdhury.com)

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 do you think there are? Drop a line and share with us. Whatever the case, the new features are really very exciting. Isn’t it? Which feature did you like 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

http://www.kunal-chowdhury.com
You may like to follow me on twitter @kunal2383 or may like the Facebook page of my blog http://www.facebook.com/blog.kunal.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:


You may also be interested in...

Comments and Discussions

 
QuestionC# 6.0 just suxx Pin
Thornik29-Dec-14 9:07
memberThornik29-Dec-14 9:07 
AnswerRe: C# 6.0 just suxx Pin
cdengler29-Dec-14 9:23
membercdengler29-Dec-14 9:23 
QuestionExceptions? Pin
27-Dec-14 20:13
suss27-Dec-14 20:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181117.1 | Last Updated 27 Dec 2014
Article Copyright 2014 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid