In VB.NET 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.
Sort
exampleTo demonstrate lambda syntax in VB.NET, we use a lambda as an argument to Array.Sort
. Array.Sort()
requires a Comparison
method (which can be specified as a lambda).
CompareTo
.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 Console.WriteLine(item) Next End Sub End Modulecat lion mouse
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
.
AddressOf
-based expression anywhere a lambda expression might be used.FindIndex
, part of the List
class
, with an AddressOf
reference to an Odd()
function.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) values.Add(10) values.Add(13) values.Add(20) ' Find first odd number's index. Dim i As Integer = values.FindIndex(AddressOf Odd) Console.WriteLine(i) End Sub End Module1
Func
, 1 argumentHere we see the first lambda expression. The local variable Func
(func1) is assigned to a lambda that receives one Integer and returns an Integer.
Sub
keywords.Sub
returns no value.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 ' Use Func. Console.WriteLine(func1.Invoke(4)) End Sub End Module5
Func
, 2 argumentsThis second example is similar to the first one, but a bit more complex. The lambda (func2) receives two Integers and also returns an Integer.
Module Module1 Sub Main() ' 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 ' Use the function. Console.WriteLine(func2.Invoke(2, 3)) End Sub End Module6
Func
, simple syntaxHere we see the third lambda. This lambda (func3) uses the abbreviated syntax. Here we omit the type of the formal parameter and the End Function statement.
Module Module1 Sub Main() ' Lambda expression that receives Integer, returns String. ' ... Short syntax. Dim func3 As Func(Of Integer, String) = Function(x) (x + 1).ToString() ' Call the lambda. Console.WriteLine(func3.Invoke(3)) End Sub End Module4
An Action, like a Sub
, returns no value. Here we see the final lambda. We see the full syntax for a Sub()
lambda expression. We can use parameters or abbreviated syntax.
Module Module1 Sub Main() ' Lambda expression that returns void. Dim action1 As Action = Sub() Console.WriteLine("action1") End Sub ' Use Action instance. action1.Invoke() End Sub End Moduleaction1
There is no low-level difference between lambda expressions and regular Functions that are assigned with AddressOf
. Lambda expressions thus are syntactic sugar.
List
methods (Sort
, Find
).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.
Lambdas are also essential for many LINQ methods. Lambdas are useful for implementing delegates. They fill many important requirements.