What is Lambda Expression ?
Anonymous function that takes no parameter and return nothing can be written in form of Lambda.
In descriptive terms Lambda expressions are a simpler syntax for anonymous delegates and can be used everywhere
an anonymous delegate can be used. However, the opposite is not true; lambda expressions can be converted to
expression trees which allows for a lot of the magic like LINQ to SQL. The following is an example of a LINQ to
Objects expression using anonymous delegates then lambda expressions to show how much easier on the eye they are:
// anonymous delegate
var evens = Enumerable
.Range(1, 100)
.Where(delegate(int x) { return (x % 2) == 0; })
.ToList();
// lambda expression
var evens = Enumerable
.Range(1, 100)
.Where(x => (x % 2) == 0)
.ToList();
Lambda expressions and anonymous delegates have an advantage over writing a separate function: they implement closures which can allow you to pass local state to the function without adding parameters to the function or creating one-time-use objects. Expression trees are a very powerful new feature of C# 3.0 that allow an API to look at the structure of an expression instead of just getting a reference to a method that can be executed.
An API just has to make a delegate parameter into an Expression parameter and the compiler will generate an expression tree from a lambda instead of an anonymous delegate:
void Example(Predicate aDelegate);
called like:
Example(x => x > 5);
becomes:
void Example(Expression> expressionTree);
The latter will get passed a representation of the abstract syntax tree that describes the expression x > 5.
When & Where to use Lambda Expression?
Essentially, the lambda expression provides a shorthand for the compiler to emit methods and assign them
to delegates; this is all done for you. The benefit you get with a lambda expression that you don't get
from a delegate/function combination is that the compiler performs automatic type inference on the lambda
arguments.
0 Comment(s)