Sub Expression (Visual Basic)
Updated: July 20, 2015
For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.
Declares the parameters and code that define a subroutine lambda expression.
Sub ( [ parameterlist ] ) statement - or - Sub ( [ parameterlist ] ) [ statements ] End Sub
|Optional. A list of local variable names that represent the parameters of the procedure. The parentheses must be present even when the list is empty. For more information, see Parameter List.|
|Required. A single statement.|
|Required. A list of statements.|
A lambda expression is a subroutine that does not have a name and that executes one or more statements. You can use a lambda expression anywhere that 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 subroutine. The differences are as follows:
A lambda expression does not have a name.
A lambda expression cannot have a modifier, such as
The body of a single-line lambda expression must be a statement, not an expression. The body can consist of a call to a sub procedure, but not a call to a function procedure.
In a lambda expression, either all parameters must have specified data types or all parameters must be inferred.
ParamArrayparameters are not permitted in lambda expressions.
Generic parameters are not permitted in lambda expressions.
Following is an example of a lambda expression that writes a value to the console. The example shows both the single-line and multiline lambda expression syntax for a subroutine. For more examples, see Lambda Expressions.
Dim writeline1 = Sub(x) Console.WriteLine(x) Dim writeline2 = Sub(x) Console.WriteLine(x) End Sub ' Write "Hello". writeline1("Hello") ' Write "World" writeline2("World")