Can you explain lambda expressions?

The answers to this question might be useful to you.


Basically as far as C# is concerned, lambda expressions are an easy way to create a delegate (or an expression tree, but let's leave those aside for now).

In C# 1 we could only create delegate instances from normal methods. In C# 2 we gained anonymous methods. In C# 3 we gained lambda expressions, which are like more concise anonymous methods.

They're particularly concise when you want to express some logic which takes one value and returns a value. For instance, in the context of LINQ:

                       // Only include children - a predicate
var query = dataSource.Where(person => person.Age < 18) 
                       // Transform to sequence of names - a projection
                      .Select(person => person.Name);

There's a fuller discussion of this - along with other aspects - in my article on closures.


lambda functions are just anonymous functions.

For example, in python, you want to double all elements in a list. There are pretty much three ways to do so:

List-expressions:

[2*x for x in list]

explicit function:

def double(x):
    return 2*x
map(double, list) # iirc

with lambdas:

double = lambda x : 2*x
map(double, list)

So, a lambda in most languages is just a way to avoid the syntactic overhead of creating a new function.