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.

232 CHAPTER 9 Lambda expressions and expression trees9.1 Lambda expressions as delegatesIn many ways, lambda expressions can be seen as an evolution of anonymous methodsfrom <strong>C#</strong> 2. There’s almost noth<strong>in</strong>g that an anonymous method can do that can’t be doneus<strong>in</strong>g a lambda expression, and it’s almost always more readable and compact us<strong>in</strong>glambdas. In particular, the behavior of captured variables is exactly the same <strong>in</strong> lambdaexpressions as <strong>in</strong> anonymous methods. In their most explicit form, not much differenceexists between the two—but lambda expressions have a lot of shortcuts available to makethem compact <strong>in</strong> common situations. Like anonymous methods, lambda expressionshave special conversion rules—the type of the expression isn’t a delegate type <strong>in</strong> itself,but it can be converted <strong>in</strong>to a delegate <strong>in</strong>stance <strong>in</strong> various ways, both implicitly andexplicitly. The term anonymous function covers anonymous methods and lambda expressions—<strong>in</strong>many cases the same conversion rules apply to both of them.We’re go<strong>in</strong>g to start with a very simple example, <strong>in</strong>itially expressed as an anonymousmethod. We’ll create a delegate <strong>in</strong>stance that takes a str<strong>in</strong>g parameter and returns an<strong>in</strong>t (which is the length of the str<strong>in</strong>g). First we need to choose a delegate type to use;fortunately, .NET 3.5 comes with a whole family of generic delegate types to help us out.9.1.1 Prelim<strong>in</strong>aries: <strong>in</strong>troduc<strong>in</strong>g the Func delegate typesThere are five generic Func delegate types <strong>in</strong> the System namespace of .NET 3.5.There’s noth<strong>in</strong>g special about Func—it’s just handy to have some predef<strong>in</strong>ed generictypes that are capable of handl<strong>in</strong>g many situations. Each delegate signature takesbetween zero and four parameters, the types of which are specified as type parameters.The last type parameter is used for the return type <strong>in</strong> each case. Here are the signaturesof all the Func delegate types:public delegate TResult Func()public delegate TResult Func(T arg)public delegate TResult Func(T1 arg1, T2 arg2)public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3)public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4)For example, Func is equivalent to a delegate type of the formdelegate <strong>in</strong>t SomeDelegate(str<strong>in</strong>g arg1, double arg2)The Action set of delegates provide the equivalent functionality when you want avoid return type. The s<strong>in</strong>gle parameter form of Action existed <strong>in</strong> .NET 2.0, but the restare new to .NET 3.5. For our example we need a type that takes a str<strong>in</strong>g parameterand returns an <strong>in</strong>t, so we’ll use Func.9.1.2 First transformation to a lambda expressionNow that we know the delegate type, we can use an anonymous method to create ourdelegate <strong>in</strong>stance. List<strong>in</strong>g 9.1 shows this, along with execut<strong>in</strong>g the delegate <strong>in</strong>stanceafterward so we can see it work<strong>in</strong>g.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!