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 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.


 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

17 comments

  1. I think the old way is clearer thus better

    ReplyDelete
  2. Less is not always more.

    ReplyDelete
  3. I don't like where that one is going.

    ReplyDelete
  4. Same here, clarity over brevity

    ReplyDelete
  5. Reminds me of F# and Scala, so it doesn't seem unclear to me. It could be abused, but so could just about anything.

    ReplyDelete
  6. A new feature with not much to be excited about.

    public string OldLog(string str) => Console.WriteLine(str);
    i could write it with the old as
    public string OldLog(string str) { return Console.WriteLine(str); }
    still easier to read due to curly brackets.

    just my 2 cents.

    :)

    ReplyDelete
  7. Implementing ideas from apple languages. Good work !

    ReplyDelete
  8. wow... I'm really surprised how many people are saying: "why not do it the way we always have?"

    I'm a really big fan of expression bodied functions. May seem just syntax sugar, but in fact they encourage you to write shorter, side-effect free functions - essentially to code in a functional style.

    ReplyDelete
    Replies
    1. It's not really reducing lines of code, and it can cause syntax confusion and you are getting no real benefit. Its crap basically...

      Delete
  9. The hate is real. " Its crap basically..." keked from hater. Dont like it - dont use it, stop whining.

    ReplyDelete
  10. I think this is a great addition to the language.
    More code brevity = more code on your screen = easier to read. Simple as that. There's nothing unclear about this syntax!

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. Lot of haters. Seems to be they just don't want to change because why change...
    I like it, the reason is simple. When I see brackets I think scope. When I see a lambda I think assignment or simple function. If I see brackets I know this method is slightly more than trivial. Beautiful! Elegant! I like it!

    ReplyDelete
  13. I like it, much cleaner code and easy to implement.

    ReplyDelete
  14. It really is JUST syntactic sugar. I don't hate it but I don't prefer it, either. Won't go changing my code just to use it but when I write new code I will use it, IF I remember. :-)

    ReplyDelete
  15. Your examples don't really shed good light to the usage... here is a more concrete example of the advantage.

    public int Age
    {
    get { return GetValue(() => Age); }
    set { SetValue(() => Age, value); }
    }

    public Gender Gender
    {
    get { return GetValue(() => Gender); }
    set { SetValue(() => Gender, value); }
    }

    ReplyDelete
  16. I'm only interested/liking in using them in place for getters as thusly:

    Previous getter:
    public int FontSize
    {
    get { return (fontSize); }
    }

    Newer lamba:
    public string FontName => (fontName);

    :)

    ReplyDelete


 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top