One of the newest features of Visual Basic ... new in VB.NET 2008 ... is called lamba expressions. This might be an unfortunate name because it makes them more confusing than they have to be.
Here's the key: Lambda expressions are simply delegate functions.
But they have a new syntax that allows the VB.NET compiler to do a lot of work that you had to do in your own code in previous versions of Visual Basic. The essence of lamba expressions is a way to get the compiler to give you more help.
----------------------
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) <function definition>
To introduce the concept, let's define a lambda that simply checks a string.
Dim checkName As Func(Of String, Boolean) = _
Function(x As String) _
If(x = "Cabernet Sauvignon", True, False)
The "lamda 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 are new in VB.NET 2008.)
You could use this lambda throughout your program 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 it more clear exactly what a lambda expression actually is.
Since lambda expressions are simply delegates, you can use them anywhere you use delegates. (See Using Delegates in Visual Basic .NET for more.) The code above can also be reworked slightly to make this stand out.
Delegate Function myCheck(ByVal x As String) As Boolean
Sub mySub()
Dim myDelegate As myCheck = Function(x As String) _
If(x = "Cabernet Sauvignon", True, False)
Another feature of VB.NET 2008, and lamba expressions, is inferred types. Recode this again (without the Func which we know returns a Boolean in this case) as ...
Dim checkName = Function(x As String) _
If(x = "Cabernet Sauvignon", True, False)
Now when you check the type of checkName, you'll see that it's Boolean in spite of the fact that it was never declared that way. The VB.NET compiler looks at the Ternary operator, figures out that it returns a Boolean, and creates the lamba expression as a Boolean type.
Lambda expressions had to be added to Visual Basic 2008 to support Language Integrated Queries (LINQ). You won't see them directly there, but the compiler generates them anyway.
One limitation, right now, of a lambda expression is that it must be just one single expression. You can't put multiple lines of code in them. But expect that to change in the next version of VB.NET!

