O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required