13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Expression trees241So, what’s the po<strong>in</strong>t of do<strong>in</strong>g this? Well, LambdaExpression has a Compile method thatcreates a delegate of the appropriate type. This delegate can now be executed <strong>in</strong> thenormal manner, as if it had been created us<strong>in</strong>g a normal method or any other means.List<strong>in</strong>g 9.7 shows this <strong>in</strong> action, with the same expression as before.List<strong>in</strong>g 9.7Compil<strong>in</strong>g and execut<strong>in</strong>g an expression treeExpression firstArg = Expression.Constant(2);Expression secondArg = Expression.Constant(3);Expression add = Expression.Add(firstArg, secondArg);Func compiled = Expression.Lambda(add).Compile();Console.WriteL<strong>in</strong>e(compiled());Arguably list<strong>in</strong>g 9.7 is one of the most convoluted ways of pr<strong>in</strong>t<strong>in</strong>g out “5” that youcould ask for. At the same time, it’s also rather impressive. We’re programmaticallycreat<strong>in</strong>g some logical blocks and represent<strong>in</strong>g them as normal objects, and then ask<strong>in</strong>gthe framework to compile the whole th<strong>in</strong>g <strong>in</strong>to “real” code that can be executed.You may never need to actually use expression trees this way, or even build them upprogrammatically at all, but it’s useful background <strong>in</strong>formation that will help youunderstand how LINQ works.As I said at the beg<strong>in</strong>n<strong>in</strong>g of this section, expression trees are not too far removedfrom CodeDOM—Snippy compiles and executes <strong>C#</strong> code that has been entered aspla<strong>in</strong> text, for <strong>in</strong>stance. However, two significant differences exist between CodeDOMand expression trees.First, expression trees are only able to represent s<strong>in</strong>gle expressions. They’re notdesigned for whole classes, methods, or even just statements. Second, <strong>C#</strong> supportsexpression trees directly <strong>in</strong> the language, through lambda expressions. Let’s take alook at that now.9.3.3 Convert<strong>in</strong>g <strong>C#</strong> lambda expressions to expression treesAs we’ve already seen, lambda expressions can be converted to appropriate delegate<strong>in</strong>stances, either implicitly or explicitly. That’s not the only conversion that is available,however. You can also ask the compiler to build an expression tree from yourlambda expression, creat<strong>in</strong>g an <strong>in</strong>stance of Expression at executiontime. For example, list<strong>in</strong>g 9.8 shows a much shorter way of creat<strong>in</strong>g the “return 5”expression, compil<strong>in</strong>g it and then <strong>in</strong>vok<strong>in</strong>g the result<strong>in</strong>g delegate.List<strong>in</strong>g 9.8Us<strong>in</strong>g lambda expressions to create expression treesExpression return5 = () => 5;Func compiled = return5.Compile();Console.WriteL<strong>in</strong>e(compiled());In the first l<strong>in</strong>e of list<strong>in</strong>g 9.8, the () => 5 part is the lambda expression. In this case,putt<strong>in</strong>g it <strong>in</strong> an extra pair of parentheses around the whole th<strong>in</strong>g makes it look worserather than better. Notice that we don’t need any casts because the compiler can verifyLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!