VB.NET Lambda Expression

The VB.NET programming language

You want to use lambda expressions in your VB.NET program. With lambda expressions, you can declare and implement an entire Function or Sub directly inside another method. The VB.NET language provides special syntax forms for this purpose.

This VB tutorial shows lambda expression syntax. It provides example code.

Examples

First, this example uses four lambda expressions. It uses these expressions to implement Func and Action instances. In the example, func1, func2, func3, and action1 are all assigned to lambda expressions. The lambda expressions are found after the = sign and start with the Function or Sub keywords.

Program that uses lambda expressions [VB.NET]

Module Module1
    Sub Main()
	' Lambda expression that receives Integer, returns Integer.
	Dim func1 As Func(Of Integer, Integer) = Function(value As Integer)
						     Return value + 1
						 End Function

	' Lambda expression that receives two Integers, returns Integer.
	Dim func2 As Func(Of Integer, Integer, Integer) =
	    Function(value As Integer, value2 As Integer)
		Return value * value2
	    End Function

	' Lambda expression that receives Integer, returns String.
	' ... Short syntax.
	Dim func3 As Func(Of Integer, String) = Function(x) (x + 1).ToString()

	' Lambda expression that returns void.
	Dim action1 As Action = Sub()
				    Console.WriteLine("action1")
				End Sub

	' Use Func and Action instances.
	Console.WriteLine(func1.Invoke(4))
	Console.WriteLine(func2.Invoke(2, 3))
	Console.WriteLine(func3.Invoke(3))
	action1.Invoke()
    End Sub
End Module

Output

5
6
4
action1

First two lambda expressions. In the example, the first two lambdas use the verbose Function syntax. The first example func1 is assigned to a lambda that receives one Integer and returns an Integer. The second example receives two Integers and also returns an Integer.

Third example. The third lambda expression uses the abbreviated syntax. With this syntax, you do not need to specify the type of the formal parameter. You do not need to use the End Function statement either. There is no difference in the actual code generated with this syntax.

Final Sub example. The final example shows the full syntax for a Sub() lambda expression. As with Function, you can use formal parameters (not shown) or the abbreviated syntax (also not shown). At the end of Main, we simply test all the delegate instances that were implemented with lambda expressions.

Question and answer

Syntactic sugar

What is so special about lambda expressions? There is actually no low-level difference between lambda expressions and regular Functions that are assigned with AddressOf. Lambda expressions are syntactic sugar, which means they are a special syntax form in the VB.NET language that allows you to write more concise and clear programs.

AddressOf Operator Syntactic Sugar

Summary

Lambda expressions are used throughout many new VB.NET programs. Lambda expressions are important for using the List type's searching methods; they are also essential for many LINQ methods. Finally, they are useful for implementing delegates, as we saw in this example.

List Find Function: FindIndex, FindLast VB.NET Tutorials
.NET