| Start |
|---|
| Using lambda syntax to create a closure See remarks for discussion of closures. Suppose we have an interface: and then the following is executed: Now Warning: this can mean that the same function call returns different values at different times (e.g. In this example if the machine keeps a sum of its inputs). In lots of cases, this may be unexpected and is to be avoided for any code in a functional style - accidental and unexpected closures can be a source of bugs. |
Basic lambda expressions |
Basic lambda expressions with LINQ |
Lambda syntax with statement block body |
Lambda expressions with System.Linq.Expressions RemarksClosuresLambda expressions will implicitly capture variables used and create a closure (opens new window). A closure is a function along with some state context. The compiler will generate a closure whenever a lambda expression 'encloses' a value from its surrounding context. E.g. when the following is executed
This can be important, because as long as the reference to the value now in On the other hand, closures can be used to deliberate effect to encapsulate a behaviour which involves references to other objects. E.g. Closures can also be used to model state machines: |
Read Article
https://develop.horje.com/learn/1434/reference