This tutorial covers Lambda Expressions in VB.NET.
Lambda expressions in Visual Basic (NET) are an alternative way of writing functions and sub-routines without the use of a name. You can write both single-line and multi-line lambdas expressions.
In this tutorial we’ll cover how to write lambdas in VB.NET and use them effectively in your program.
VB.NET Lambda Example
There are several different ways in which Lambdas can be declared in VB.NET. The end result is always the same though, with very slight differences that you wont notice normally. We’ll discuss at least two techniques, here in this tutorial.
This is the first way of writing Lambdas in VB.NET, using the
Module Module1 Sub Main() Dim inc = Function(x As Integer) Return x + 1 End Function Console.WriteLine(inc(4)) Console.Read() End Sub End Module
Console.Read() line stops the command prompt from shutting down before we can read the output.
Here is another alternative method. It’s shorter and a bit simple, but I think it’s better practice to have declared the return type like we did in the previous example. This code may not compile if certain checks are compiled.
Dim inc = Function(x) Return x + 1 End Function
There is no limit to the number of parameters that you may include within a VB.NET lambda expression.
Lambdas with SubRoutines
Previously we used the
Function keyword to create lambda functions. Now we’ll use the
Sub keyword to create lambda subroutines.
As a quick re-cap, Functions can return values but subroutines (or procedures) cannot. We typically use subroutines to display values or text.
Module Module1 Sub Main() Dim inc = Sub(x As Integer) Console.WriteLine(x + 1) End Sub inc(4) Console.Read() End Sub End Module
As you can see in the code above, there is no Return statement. The output is handled directly the by
WriteLine function in the SubRoutine.
Single Line Lambdas
Uptil now we’ve been creating multiline lambdas. We can further shorten and simplify the lambda expression by moving everything into a single line, like the example below.
Module Module1 Sub Main() Dim inc = Sub(x As Integer) Console.WriteLine(x + 1) inc(4) Console.Read() End Sub End Module
When writing lambdas this way, there is no need to include the
End Function or
End Sub line.
Lambda Syntax and Comparison
A short review of some basic differences between the syntax of lambdas and regular functions/subroutines.
- The ability to create single-line functions/subroutines
- A lambda expression does not have a name
- Lambda expressions do not have modifiers such as
- Regular functions use the
As DataTypekeyword to declare the return type. In lambdas, the
Askeyword is not compulsory. The return type of the lambda is automatically judged based of the code.
- Lambdas do not require an
Endkeyword (only single line lambdas)
This marks the end of the VB.NET Lambda Expressions tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.