13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

234 CHAPTER 9 Lambda expressions and expression trees9.1.3 Us<strong>in</strong>g a s<strong>in</strong>gle expression as the bodyThe form we’ve seen so far uses a full block of code to return the value. This is veryflexible—you can have multiple statements, perform loops, return from differentplaces <strong>in</strong> the block, and so on, just as with anonymous methods. Most of the time,however, you can easily express the whole of the body <strong>in</strong> a s<strong>in</strong>gle expression, the valueof which is the result of the lambda. In these cases, you can specify just that expression,without any braces, return statements, or semicolons. The format then is(explicitly-typed-parameter-list) => expressionIn our case, this means that the lambda expression becomes(str<strong>in</strong>g text) => text.LengthThat’s start<strong>in</strong>g to look simpler already. Now, what about that parameter type? Thecompiler already knows that <strong>in</strong>stances of Func take a s<strong>in</strong>gle str<strong>in</strong>gparameter, so we should be able to just name that parameter…9.1.4 Implicitly typed parameter listsMost of the time, the compiler can guess the parameter types without you explicitlystat<strong>in</strong>g them. In these cases, you can write the lambda expression as(implicitly-typed-parameter-list) => expressionAn implicitly typed parameter list is just a comma-separated list of names, without thetypes. You can’t mix and match for different parameters—either the whole list isexplicitly typed, or it’s all implicitly typed. Also, if any of the parameters are out or refparameters, you are forced to use explicit typ<strong>in</strong>g. In our case, however, it’s f<strong>in</strong>e—soour lambda expression is now just(text) => text.LengthThat’s gett<strong>in</strong>g pretty short now—there’s not a lot more we could get rid of. The parenthesesseem a bit redundant, though.9.1.5 Shortcut for a s<strong>in</strong>gle parameterWhen the lambda expression only needs a s<strong>in</strong>gle parameter, and that parameter canbe implicitly typed, <strong>C#</strong> 3 allows us to omit the parentheses, so it now has this form:parameter-name => expressionThe f<strong>in</strong>al form of our lambda expression is thereforetext => text.LengthYou may be wonder<strong>in</strong>g why there are so many special cases with lambda expressions—none of the rest of the language cares whether a method has one parameter or more,for <strong>in</strong>stance. Well, what sounds like a very particular case actually turns out to beextremely common, and the improvement <strong>in</strong> readability from remov<strong>in</strong>g the parenthesesfrom the parameter list can be significant when there are many lambdas <strong>in</strong> a shortpiece of code.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!