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.

Lambda expressions as delegates233List<strong>in</strong>g 9.1Us<strong>in</strong>g an anonymous method to create a delegate <strong>in</strong>stanceFunc returnLength;returnLength = delegate (str<strong>in</strong>g text) { return text.Length; };Console.WriteL<strong>in</strong>e (returnLength("Hello"));List<strong>in</strong>g 9.1 pr<strong>in</strong>ts “5,” just as we’d expect it to. I’ve separated out the declaration ofreturnLength from the assignment to it so we can keep it on one l<strong>in</strong>e—it’s easier tokeep track of that way. The anonymous method expression is the part <strong>in</strong> bold, andthat’s the part we’re go<strong>in</strong>g to convert <strong>in</strong>to a lambda expression.The most long-w<strong>in</strong>ded form of a lambda expression is this:(explicitly-typed-parameter-list) => { statements }The => part is new to <strong>C#</strong> 3 and tells the compiler that we’re us<strong>in</strong>g a lambda expression.Most of the time lambda expressions are used with a delegate type that has a nonvoidreturn type—the syntax is slightly less <strong>in</strong>tuitive when there isn’t a result. This isanother <strong>in</strong>dication of the changes <strong>in</strong> idiom between <strong>C#</strong> 1 and <strong>C#</strong> 3. In <strong>C#</strong> 1, delegateswere usually used for events and rarely returned anyth<strong>in</strong>g. Although lambda expressionscerta<strong>in</strong>ly can be used <strong>in</strong> this way (and we’ll show an example of this later), muchof their elegance comes from the shortcuts that are available when they need toreturn a value.With the explicit parameters and statements <strong>in</strong> braces, this version looks very similarto an anonymous method. List<strong>in</strong>g 9.2 is equivalent to list<strong>in</strong>g 9.1 but uses a lambdaexpression.List<strong>in</strong>g 9.2A long-w<strong>in</strong>ded first lambda expression, similar to an anonymous methodFunc returnLength;returnLength = (str<strong>in</strong>g text) => { return text.Length; };Console.WriteL<strong>in</strong>e (returnLength("Hello"));Aga<strong>in</strong>, I’ve used bold to <strong>in</strong>dicate the expression used to create the delegate <strong>in</strong>stance.When read<strong>in</strong>g lambda expressions, it helps to th<strong>in</strong>k of the => part as “goes to”—so theexample <strong>in</strong> list<strong>in</strong>g 9.2 could be read as “text goes to text.Length.” As this is the onlypart of the list<strong>in</strong>g that is <strong>in</strong>terest<strong>in</strong>g for a while, I’ll show it alone from now on. You canreplace the bold text from list<strong>in</strong>g 9.2 with any of the lambda expressions listed <strong>in</strong> thissection and the result will be the same.The same rules that govern return statements <strong>in</strong> anonymous methods apply tolambdas too: you can’t try to return a value from a lambda expression with a voidreturn type, whereas if there’s a nonvoid return type every code path has to return acompatible value. 2 It’s all pretty <strong>in</strong>tuitive and rarely gets <strong>in</strong> the way.So far, we haven’t saved much space or made th<strong>in</strong>gs particularly easy to read. Let’sstart apply<strong>in</strong>g the shortcuts.2Code paths throw<strong>in</strong>g exceptions don’t need to return a value, of course, and neither do detectable <strong>in</strong>f<strong>in</strong>iteloops.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!