http://msdn.microsoft.com/en-us/library/bb763133.aspx
Module Module1
Sub Main()
Dim array1 As Func(Of Integer)() = New Func(Of Integer)(4) {}
For i As Integer = 0 To 4
array1(i) = Function() i
Next
For Each funcElement In array1
System.Console.WriteLine(funcElement())
Next
End Sub
End Module
It says the result will always be 5 namely the final value of i. How come?
They don’t put the iteration variable in the “closure”?
The problem occurs because lambda expressions do not execute when they are constructed but rather when they are invoked.
See the link below:
http://blogs.msdn.com/b/vbteam/archive/2007/07/26/closures-in-vb-part-5-looping.aspx
Hope it helps.