Lexical Closures

To provide support for lambda expressions, the Visual Basic compiler implements a background feature known as lexical closures. Before going into the explanation, remember that you will not generally use closures in your code because they are typically generated for compiler use only, but it’s important to know what they are and what they do. Basically lexical closures allow access to the same class-level variable to multiple functions and procedures. A code example provides a better explanation. Consider the following code, in which the Divide method takes advantage of a lambda expression to calculate the division between two numbers:

Because both the Divide method and its lambda expression have access to the x local variable, ...

Get Visual Basic® 2010 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.