C# 6 Features: Expression Bodied Methods

by Joe on Feb 4th in .Net, C#

Ok this is going to be a short one. We’ve seen how the Expression Bodied Auto Properties can simplify things a little bit. But, if that is possible, then what about other things with simple bodies? Turns out, Methods are fair game as well.

This is turning in to a fairly popular language change. Again, it’s not a significant change to how C# works, or how you write code, it’s essentially syntactic sugar that makes life just a little bit easier.

It lets us take code like this:

And turn it into code that looks like this:

It’s not a huge change. I wouldn’t go out and re-write production applications in order to make use of it. But, it will simplify things moving forward, and perhaps make things just a little more readable.

Also, some of the folks in the F# world seem to think this is a move towards a more “functional language” paradigm, and that this is a good thing. We shall see.

2 Comments

  1. BobT

    10th February 2016

    Syntactic sugar is all well and good, but it does make developers forget to validate input, such as what would happen if the pdfFile parameter is null or blank. It’s true that a sanity precondition check isn’t in the original example but I regularly find that it’s also not thought about in the syntactic sugar equivalent, meaning it’s even more difficult to identify null reference exceptions and the like without enforcing design-by-contract. Personally I find for that very reason too much sugar is bad for you ;-

    • Joe

      18th February 2016

      I agree to a large extent – it’s important to know what’s going on, and to make sure you use the right tool for the job. The Null-Conditional and Static Using features are definitely in my “Use, but be wary” category, whereas the String Interpolation and Auto Property Initialization fall into the “Use, think about it, but you’re likely better off this way than the old way” category. But that’s really long, so shorten it to “Go for it”.

Leave a Reply

Powered By Wordpress Designed By Ridgey