The Wayback Machine - http://web.archive.org/web/20160619082421/http://visualbasic.about.com/od/usingvbnet/a/lambdaexpr.htm

Getting Started With Lamda Expressions in VB.NET

A introduction to VB.NET's unnamed function expressions.

A new feature was added to VB.NET 2008 called lamba expressions. It's a cute name, but because it doesn't really mean anything to most people initially who aren't professional mathematicians, it makes lamda expressions more confusing than they have to be.

Here's the key to understanding them: Lambda expressions are simply unnamed delegate functions.

The main benefit of lambda expressions is that they make the VB.NET compiler do a lot of work that you had to do in your own code before.

They were also required to make LINQ work.

----------------------

Historical Note - Where the name "lamba expression" came from:

Lambda calculus is a formal math system created by Alonzo Church and Stephen Cole Kleene in the 1930s. I wrote about Kleene in my article about regular expressions. The lambda calculus is used to define and study computable functions.

The mathematical foundation of .NET's lamba expressions are based on this work. Microsoft's Paul Vick has complained, "I might be tempted to say 'You know, we should really call them inline functions instead of lambda expressions, because I think inline function is a little clearer and less of a computer science-y term. ... (Do you detect a trace of bitter sarcasm there? Perhaps just a little.)"

----------------------

The syntax of a lamda expression is actually super simple:


Function(parameter) 

To introduce the concept, let's define an "named expression" (This isn't a lambda because it's named.) that simply checks a string.


Dim checkName As Func(Of String, Boolean) =
    Function(x As String) _
       If(x = "Cabernet Sauvignon", True, False)

The named expression is the part on the right side of the equal sign. (I'm also using the If Ternary operator and the Func delegate; both were also new in VB.NET 2008.)

You could use this expression throughout your program to check whether a string is exactly equal to "Cabernet Sauvignon" like this:


If checkName(myStringVar) Then ...

The fact that you could do the same thing with a regular function definition ...


Function checkName(ByVal x As String) As Boolean
    Return If(x = "Cabernet Sauvignon", True, False)
End Function

... makes what is happening here more clear. But these are still not lambda expressions because, using this syntax, it's necessary to code a separate Function (or Sub) that can be called with its own unique name. This is often overkill since you may only want to execute the function in one place. A lambda lets you simply code the same thing "inline" without giving it a name. Here's what a lambda that does the same thing looks like:


Console.WriteLine(
    (Function(x As String) _
         x = "Cabernet Sauvignon")(myStringVar))

Output:

-- Named Expression --
True
-- Lambda Expression --
True

Wow! The whole thing in just one statement. (There are three lines here because long lines don't fit well on some web pages.)

Notice that both return a boolean type (True, in this case, after converting to string). There is no Return for a lambda function. The return type is determined by the type of the body of the lambda, in this case, the body is:


x = "Cabernet Sauvignon"

By the way, in this context, this is a conditional, not an assignment statement.

Since lambda expressions are simply delegates, you can use them anywhere you use delegates (except RemoveHandler). (See Using Delegates in Visual Basic .NET for more.)