Compiler-Generated Expression Trees

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

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.