| title | Function Expression (Visual Basic) | |||
|---|---|---|---|---|
| ms.date | 07/20/2015 | |||
| ms.prod | .net | |||
| ms.reviewer | ||||
| ms.suite | ||||
| ms.technology |
|
|||
| ms.topic | article | |||
| helpviewer_keywords |
|
|||
| ms.assetid | e8a47a45-4b8a-4f45-a623-7653625dffbc | |||
| caps.latest.revision | 18 | |||
| author | dotnet-bot | |||
| ms.author | dotnetcontent |
Declares the parameters and code that define a function lambda expression.
Function ( [ parameterlist ] ) expression
- or -
Function ( [ parameterlist ] )
[ statements ]
End Function
| Term | Definition |
|---|---|
parameterlist |
Optional. A list of local variable names that represent the parameters of this procedure. The parentheses must be present even when the list is empty. See Parameter List. |
expression |
Required. A single expression. The type of the expression is the return type of the function. |
statements |
Required. A list of statements that returns a value by using the Return statement. (See Return Statement.) The type of the value returned is the return type of the function. |
A lambda expression is a function without a name that calculates and returns a value. You can use a lambda expression anywhere you can use a delegate type, except as an argument to RemoveHandler. For more information about delegates, and the use of lambda expressions with delegates, see Delegate Statement and Relaxed Delegate Conversion.
The syntax of a lambda expression resembles that of a standard function. The differences are as follows:
-
A lambda expression does not have a name.
-
Lambda expressions cannot have modifiers, such as
OverloadsorOverrides. -
Lambda expressions do not use an
Asclause to designate the return type of the function. Instead, the type is inferred from the value that the body of a single-line lambda expression evaluates to, or the return value of a multiline lambda expression. For example, if the body of a single-line lambda expression isWhere cust.City = "London", its return type isBoolean. -
The body of a single-line lambda expression must be an expression, not a statement. The body can consist of a call to a function procedure, but not a call to a sub procedure.
-
Either all parameters must have specified data types or all must be inferred.
-
Optional and Paramarray parameters are not permitted.
-
Generic parameters are not permitted.
The following examples show two ways to create simple lambda expressions. The first uses a Dim to provide a name for the function. To call the function, you send in a value for the parameter.
[!code-vbVbVbalrLambdas#1]
[!code-vbVbVbalrLambdas#2]
Alternatively, you can declare and run the function at the same time.
[!code-vbVbVbalrLambdas#3]
Following is an example of a lambda expression that increments its argument and returns the value. The example shows both the single-line and multiline lambda expression syntax for a function. For more examples, see Lambda Expressions.
[!code-vbVbVbalrLambdas#14]
Lambda expressions underlie many of the query operators in [!INCLUDEvbteclinqext], and can be used explicitly in method-based queries. The following example shows a typical [!INCLUDEvbteclinq] query, followed by the translation of the query into method format.
Dim londonCusts = From cust In db.Customers
Where cust.City = "London"
Select cust
' This query is compiled to the following code:
Dim londonCusts = db.Customers.
Where(Function(cust) cust.City = "London").
Select(Function(cust) cust) For more information about query methods, see Queries. For more information about standard query operators, see Standard Query Operators Overview.
Function Statement
Lambda Expressions
Operators and Expressions
Statements
Value Comparisons
Boolean Expressions
If Operator
Relaxed Delegate Conversion