Cheat Sheet: Delegates, Lambdas & Closures in Delegate Arrays VB.NET
What is a Delegate?
A delegate is a type that represents references to methods with a specific signature. It allows methods to be passed as parameters.
🔹 VB.NET Basic Delegate Example
Delegate Function MathOperation(x As Integer, y As Integer) As Integer
Module Program
Function Add(x As Integer, y As Integer) As Integer
Return x + y
End Function
Sub Main()
Dim op As MathOperation = AddressOf Add ' Assign method to delegate
Console.WriteLine(op(3, 5)) ' Output: 8
End Sub
End Module
Why use Delegates?
- Allows passing functions as parameters.
- Enables flexible function execution.
- Forms the basis for events and callbacks.
Storing Lambdas in a Delegate Array
VB.NET Example
Delegate Function MathOperation(x As Integer, y As Integer) As Integer
Module Program
Sub Main()
Dim operations As MathOperation() = {
Function(x, y) x + y, ' Addition
Function(x, y) x - y, ' Subtraction
Function(x, y) x * y ' Multiplication
}
Console.WriteLine(operations(0)(3, 5)) ' 8 (Addition)
Console.WriteLine(operations(1)(3, 5)) ' -2 (Subtraction)
Console.WriteLine(operations(2)(3, 5)) ' 15 (Multiplication)
End Sub
End Module
Why?
- Store multiple functions dynamically.
- Execute different behaviors without if/else chains.
Using a Delegate Array with Closures
VB.NET Example
Delegate Function CounterFunction() As Integer
Module Program
Sub Main()
Dim counters As CounterFunction() = {
MakeCounter(10),
MakeCounter(100),
MakeCounter(1000)
}
Console.WriteLine(counters(0)()) ' 11
Console.WriteLine(counters(1)()) ' 101
Console.WriteLine(counters(2)()) ' 1001
End Sub
Function MakeCounter(start As Integer) As CounterFunction
Dim count As Integer = start
Return Function()
count += 1
Return count
End Function
End Function
End Module
✅ Why?
- Each closure remembers its own state.
- Different instances operate independently.
Looping Over Delegate Arrays Dynamically
VB.NET Example
For Each operation In operations
Console.WriteLine(operation(6, 2))
Next
Why?
- Applies multiple operations to the same data.
- Removes the need for hardcoded function calls.
VB.NET Example: Delegate Array
Delegate Sub FooDelegate()
Module Program
Sub Method1()
Console.WriteLine("Method1 called")
End Sub
Sub Method2()
Console.WriteLine("Method2 called")
End Sub
Sub Main()
Dim fooArray As FooDelegate() = { AddressOf Method1, AddressOf Method2 }
fooArray(0)() ' Calls only Method1
fooArray(1)() ' Calls only Method2
End Sub
End Module
VB.NET Example: Method Chaining (Delegate.Combine
)
Delegate Sub FooDelegate()
Module Program
Sub Method1()
Console.WriteLine("Method1 called")
End Sub
Sub Method2()
Console.WriteLine("Method2 called")
End Sub
Sub Main()
Dim foo As FooDelegate = AddressOf Method1
foo = DirectCast([Delegate].Combine(foo, AddressOf Method2), FooDelegate)
foo() ' Calls both Method1 and Method2
End Sub
End Module
✅ Key Differences:
Feature | Delegate Array { f1, f2 } |
Method Chaining (+= ) |
---|---|---|
Separate Invocations | ✅ Yes, each function is independent | ❌ No, all methods execute together |
Single Delegate Execution | ✅ Yes, one method per call | ❌ No, all chained methods execute |
Can Remove Methods (-= ) |
❌ No | ✅ Yes |
Iteration Required to Call All | ✅ Yes (foreach ) |
❌ No |
✅ When to Use?
Use Case | Delegate Array { f1, f2 } |
Method Chaining (+= ) |
---|---|---|
Executing a list of independent functions | ✅ Yes | ❌ No |
Combining methods into a single call | ❌ No | ✅ Yes |
Dynamically adding/removing methods at runtime | ❌ No | ✅ Yes (+= , -= ) |
🚀 Summary
Feature | C# | VB.NET |
---|---|---|
Basic Delegate Assignment | ✅ Yes | ✅ Yes |
Store lambdas in a delegate array | ✅ Yes | ✅ Yes |
Store closures in a delegate array | ✅ Yes | ✅ Yes |
Loop through the array dynamically | ✅ Yes | ✅ Yes |
Independent function instances in an array | ✅ Yes | ✅ Yes |
👉 Verdict: Lambdas and closures work great with delegate arrays! They allow for dynamic function storage, flexible execution, and independent function instances. 🚀