Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

VB.NET Lambda Expressions

This VB.NET tutorial shows lambda expression syntax. It uses the Function keyword and provides example code.

Lambdas. A function is a data object. A lambda expression is used to specify a function.

With lambda syntax, we pass Functions as arguments, creating complex behaviors.

Higher-order procedures are functions passed as arguments. Lambdas are higher-order procedures. For lambdas, the VB.NET language has a special syntax form.

Here, we use four lambda expressions. With them, we implement Func and Action instances. In the example, func1, func2, func3, and action1 are all assigned to lambda expressions.

Tip: The lambda expressions are found after the equals sign and start with the Function or Sub keywords.

Tip 2: A Function is a method that returns a value. In contrast a Sub returns no value.

Based on:

.NET 4

VB.NET program that uses lambda expressions

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()
				End Sub

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



Some review. The first example (func1) is assigned to a lambda that receives one Integer and returns an Integer. The second (func2) receives two Integers and also returns an Integer.


And: The third lambda (func3) uses the abbreviated syntax. Here we omit the type of the formal parameter and the End Function statement.

Sub: The final part shows the full syntax for a Sub() lambda expression. We can use parameters or abbreviated syntax.


Sort. Often we sort collections with lambda expressions. The Array.Sort Function and List Sort require a Comparison method. This is a delegate type.

SortSort List

Tip: With lambdas, we can pass a Function argument (a higher-order procedure) directly to these methods to sort collections.

However: We sort the Strings in the array by their lengths. We do this with a special lambda expression that uses CompareTo.

VB.NET program that sorts with lambda Function

Module Module1

    Sub Main()
	Dim items() As String = {"cat", "mouse", "lion"}

	' Use lambda expression to specify Comparison for Array.Sort.
	Array.Sort(items, Function(a As String, b As String)
			      Return a.Length.CompareTo(b.Length)
			  End Function)

	' Loop over sorted array.
	For Each item As String In items
    End Sub

End Module



AddressOf. Lambda expression syntax is not always required. And sometimes it is clearer, and simpler, to use the AddressOf operator. We can reference a Function by name with AddressOf.


And: We can use an AddressOf-based expression anywhere a lambda expression might be used.

FindIndex: Here I use FindIndex, part of the List class, with an AddressOf reference to an Odd() function.

VB.NET program that uses AddressOf

Module Module1

    Function Odd(ByVal value As Integer) As Boolean
	' Return true if not even number.
	Return Not value Mod 2 = 0
    End Function

    Sub Main()
	Dim values As List(Of Integer) = New List(Of Integer)

	' Find first odd number's index.
	Dim i As Integer = values.FindIndex(AddressOf Odd)
    End Sub

End Module



Concepts. There is no low-level difference between lambda expressions and regular Functions that are assigned with AddressOf. Lambda expressions thus are syntactic sugar.

Tip: They are a special syntax form in the VB.NET language. They allow us to write more concise, clearer programs.

List: Lambda expressions can be used as arguments to List methods (Sort, Find).

List Find, FindIndex

Lambda expressions are used throughout many new VB.NET programs. They are important for using a List's searching methods. They also help when sorting in nontrivial ways.

LINQ. Lambdas are also essential for many LINQ methods. Lambdas are useful for implementing delegates. They fill many important requirements.