A quick look at the lambda expression

The lambda expression is new to Visual Basic and Visual Studio 2008 and part of the new Linq support. A lambda expression is an anonymous function that can contain expressions and statements. Basically it’s a function without a name that calculates and returns a single value. It is combined with the new Func type that is essentially a delegate that has the return type specified and allows arguments to be supplied. The Func delegate type is part of the system namespace and defined in the assembly System.Core.dll. By default all new projects created with Visual Basic automatically reference this dll. The underlying type of a lambda expression is one of the generic Func delegates. This makes it possible to pass a lambda expression as a parameter without explicitly assigning it to a delegate.

We can write a simple TimesTwo lambda expression that takes an integer and returns an integer. This expression effectively takes the input number and multiplies by 2, and then returns the result.

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim TimesTwo As Func(Of Integer, Integer) = Function(Numberin As Integer) Numberin * 2

        Dim ReturnNumber As Integer

        ReturnNumber = TimesTwo(50)

      MsgBox(ReturnNumber)

    End Sub