How to: Create a Lambda Expression (Visual Basic)

A lambda expression is a function or subroutine that does not have a name. A lambda expression can be used wherever a delegate type is valid.

To create a single-line lambda expression function

  1. In any situation where a delegate type could be used, type the keyword Function, as in the following example:

    Dim add1 = Function

  2. In parentheses, directly after Function, type the parameters of the function. Notice that you do not specify a name after Function.

    Dim add1 = Function (num As Integer)

  3. Following the parameter list, type a single expression as the body of the function. The value that the expression evaluates to is the value returned by the function. You do not use an As clause to specify the return type.

    Dim add1 = Function(num As Integer) num + 1
    

    You call the lambda expression by passing in an integer argument.

    ' The following line prints 6.
    Console.WriteLine(add1(5))
    
  4. Alternatively, the same result is accomplished by the following example:

    Console.WriteLine((Function(num As Integer) num + 1)(5))
    

To create a single-line lambda expression subroutine

  1. In any situation where a delegate type could be used, type the keyword Sub, as shown in the following example.

    Dim add1 = Sub

  2. In parentheses, directly after Sub, type the parameters of the subroutine. Notice that you do not specify a name after Sub.

    Dim add1 = Sub (msg As String)

  3. Following the parameter list, type a single statement as the body of the subroutine.

    Dim writeMessage = Sub(msg As String) Console.WriteLine(msg)
    

    You call the lambda expression by passing in a string argument.

    ' The following line prints "Hello".
    writeMessage("Hello")
    

To create a multiline lambda expression function

  1. In any situation where a delegate type could be used, type the keyword Function, as shown in the following example.

    Dim add1 = Function

  2. In parentheses, directly after Function, type the parameters of the function. Notice that you do not specify a name after Function.

    Dim add1 = Function (index As Integer)

  3. Press ENTER. The End Function statement is automatically added.

  4. Within the body of the function, add the following code to create an expression and return the value. You do not use an As clause to specify the return type.

    Dim getSortColumn = Function(index As Integer)
                            Select Case index
                                Case 0
                                    Return "FirstName"
                                Case 1
                                    Return "LastName"
                                Case 2
                                    Return "CompanyName"
                                Case Else
                                    Return "LastName"
                            End Select
                        End Function
    

    You call the lambda expression by passing in an integer argument.

    Dim sortColumn = getSortColumn(0)
    

To create a multiline lambda expression subroutine

  1. In any situation where a delegate type could be used, type the keyword Sub, as shown in the following example:

    Dim add1 = Sub

  2. In parentheses, directly after Sub, type the parameters of the subroutine. Notice that you do not specify a name after Sub.

    Dim add1 = Sub (msg As String)

  3. Press ENTER. The End Sub statement is automatically added.

  4. Within the body of the function, add the following code to execute when the subroutine is invoked.

    Dim writeToLog = Sub(msg As String)
                         Dim log As New EventLog()
                         log.Source = "Application"
                         log.WriteEntry(msg)
                         log.Close()
                     End Sub
    

    You call the lambda expression by passing in a string argument.

    writeToLog("Application started.")
    

Example

A common use of lambda expressions is to define a function that can be passed in as the argument for a parameter whose type is Delegate. In the following example, the GetProcesses method returns an array of the processes running on the local computer. The Where method from the Enumerable class requires a Boolean delegate as its argument. The lambda expression in the example is used for that purpose. It returns True for each process that has only one thread, and those are selected in filteredList.

Sub Main()

    ' Create an array of running processes.
    Dim procList As Process() = Diagnostics.Process.GetProcesses

    ' Return the processes that have one thread. Notice that the type
    ' of the parameter does not have to be explicitly stated.
    Dim filteredList = procList.Where(Function(p) p.Threads.Count = 1)

    ' Display the name of each selected process.
    For Each proc In filteredList
        MsgBox(proc.ProcessName)
    Next

End Sub

The previous example is equivalent to the following code, which is written in Language-Integrated Query (LINQ) syntax:

Sub Main()

    Dim filteredQuery = From proc In Diagnostics.Process.GetProcesses
                        Where proc.Threads.Count = 1
                        Select proc

    For Each proc In filteredQuery
        MsgBox(proc.ProcessName)
    Next
End Sub

See also