Definitely one of the hot new features of C# 3 is Lambda Expressions. I agree that the name is definitely hot, and that it sounds so incredibly math cool, that I just have to use it. But, I have no idea what it is. Not having much of a formal math background, the only exposure I've had to Lambda anything was Revenge of the Nerds.
This is the result of the research I've done to figure out exactly they are what they are intended to be used for.
If you have messed around with Lambda Expressions in C# already, the following mathematical definition around Lambda Calculus provides quite a bit of clarity around the expression structure and notation.
From the mathematics standpoint, Lambda Expressions are based upon Lambda Calculus, which is a formal system that puts emphasis on function definition. In math, if you were to have the function: f(x) = x+3, it would be represented in Lambda Calculus as: λx. x + 3. If I were then to given f(2), in Lambda Calculus it would be given as (λx. x+3)2.
Evolution of Delegates to Lambda Expressions
So how does this concept benefit us in C#? In C# 1.0, we were given the feature of delegation, which allowed us to essentially pass functions as an argument to another function. Below is a really simple example of C# 1.0 delegation:
With C# 2.o, a feature was introduced called anonymous delegates. With anonymous delegates, you could do the same above but by creating the delegate on the fly:
Now, in C# 3.0, with Lambda Expressions, I can do essentially the same thing, but I get to do it a little more succinctly:
Take it even one step further, and I can get rid of the delegate declaration altogether with the new generic Func class. The Func class takes two generic arguments in it's declaration, an input type and an output type:
A Much Better Usage
You can see as the previous example progressed, it became a worse and worse example. Why not just call Console.WriteLine("Printing Number: 10"); and be done with it.
Here's a much better example using Extension Methods combined with Lambda Expressions. In this case, what I want to do is perform an operation on a collection of items. Let's use the example of an order of items, and I want to total the number of items. Here's a great way of doing that:
You can see that there's quite a bit of potential in working with Lambda Expressions. From here you can go even further into expression trees, creating a very rich language with very little code, which is a post (or ten) of it's own.
All of this has become really evident with LINQ, as it is a very powerful implementation of Lambda Expressions.