| title | How to: Create a Lambda Expression (Visual Basic) | ||
|---|---|---|---|
| ms.custom | |||
| ms.date | 07/20/2015 | ||
| ms.prod | .net | ||
| ms.reviewer | |||
| ms.suite | |||
| ms.technology |
|
||
| ms.topic | article | ||
| helpviewer_keywords |
|
||
| ms.assetid | 3279bd5c-80f7-410a-a7ba-f7085ed36aa5 | ||
| caps.latest.revision | 27 | ||
| author | dotnet-bot | ||
| ms.author | dotnetcontent |
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.
-
In any situation where a delegate type could be used, type the keyword
Function, as in the following example:Dim add1 =Function -
In parentheses, directly after
Function, type the parameters of the function. Notice that you do not specify a name afterFunction.Dim add1 = Function(num As Integer) -
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
Asclause to specify the return type.[!code-vbVbVbalrLambdas#1]
You call the lambda expression by passing in an integer argument.
[!code-vbVbVbalrLambdas#2]
-
Alternatively, the same result is accomplished by the following example:
[!code-vbVbVbalrLambdas#3]
-
In any situation where a delegate type could be used, type the keyword
Sub, as shown in the following example.Dim add1 =Sub -
In parentheses, directly after
Sub, type the parameters of the subroutine. Notice that you do not specify a name afterSub.Dim add1 = Sub(msg As String) -
Following the parameter list, type a single statement as the body of the subroutine.
[!code-vbVbVbalrLambdas#17]
You call the lambda expression by passing in a string argument.
[!code-vbVbVbalrLambdas#18]
-
In any situation where a delegate type could be used, type the keyword
Function, as shown in the following example.Dim add1 =Function -
In parentheses, directly after
Function, type the parameters of the function. Notice that you do not specify a name afterFunction.Dim add1 = Function(index As Integer) -
Press ENTER. The
End Functionstatement is automatically added. -
Within the body of the function, add the following code to create an expression and return the value. You do not use an
Asclause to specify the return type.[!code-vbVbVbalrLambdas#19]
You call the lambda expression by passing in an integer argument.
[!code-vbVbVbalrLambdas#20]
-
In any situation where a delegate type could be used, type the keyword
Sub, as shown in the following example:Dim add1 =Sub -
In parentheses, directly after
Sub, type the parameters of the subroutine. Notice that you do not specify a name afterSub.Dim add1 = Sub(msg As String) -
Press ENTER. The
End Substatement is automatically added. -
Within the body of the function, add the following code to execute when the subroutine is invoked.
[!code-vbVbVbalrLambdas#21]
You call the lambda expression by passing in a string argument.
[!code-vbVbVbalrLambdas#22]
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 xref:System.Diagnostics.Process.GetProcesses%2A method returns an array of the processes running on the local computer. The xref:System.Linq.Enumerable.Where%2A method from the xref:System.Linq.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.
[!code-vbVbVbalrLambdas#10]
The previous example is equivalent to the following code, which is written in [!INCLUDEvbteclinqext] syntax:
[!code-vbVbVbalrLambdas#11]
xref:System.Linq.Enumerable
Lambda Expressions
Function Statement
Sub Statement
Delegates
How to: Pass Procedures to Another Procedure in Visual Basic
Delegate Statement
Introduction to LINQ in Visual Basic