Compiler-Generated Expression Trees

As mentioned in Chapter 20, lambda expressions have two representations. One is the equivalent to anonymous methods, causing the lambda expression to be translated into a piece of IL code that allows direct execution of its logic:

Func<int, int, int> f = (a, b) => (a + b) * 2;

This is the same as the following C# 2.0 fragment:

Func<int, int, int> f = delegate (int a, int b) { return (a + b) * 2; };

The preceding can be translated into a separate method that’s being referred to by the delegate:

Func<int, int, int> f = new Func<int, int, int>(MyExpression);...static int MyExpression(int a, int b) { return (a + b) * 2; }

However, when we assign the lambda expression to an Expression<T> ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.