Home
Map
Lambda ExpressionsCreate lambda expressions and pass them to other methods. Use the Function keyword.
VB.NET
This page was last reviewed on Dec 30, 2023.
Lambda. In VB.NET a lambda expression is used to specify a function. With lambda syntax, we pass Functions as arguments, creating complex behaviors.
Shows a lambda
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 example. To 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).
Sort
Tip With lambdas, we can pass a Function argument (a higher-order procedure) directly to these methods to sort collections.
Here We sort the Strings in the array by their lengths. We do this with a special lambda expression that uses CompareTo.
CompareTo
Shows a lambda
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 Module
cat 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
And We can use an AddressOf-based expression anywhere a lambda expression might be used.
Detail Here I use 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 Module
1
Func, 1 argument. Here we see the first lambda expression. The local variable Func (func1) is assigned to a lambda that receives one Integer and returns an Integer.
Integer
Tip The lambda expressions are found after the equals sign and start with the Function or Sub keywords.
Info A Function is a method that returns a value. In contrast a 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 Module
5
Func, 2 arguments. This 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 Module
6
Func, simple syntax. Here 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 Module
4
Action. 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.
Sub
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 Module
action1
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.
Detail Lambda expressions can be used as arguments to List methods (Sort, Find).
List Find, Exists
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.
LINQ
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 30, 2023 (edit link).
Home
Changes
© 2007-2024 Sam Allen.