November 2019
Intermediate to advanced
404 pages
12h 9m
English
Closures are useful, but dangerous tools. Anonymous methods and lambda expressions are not always closures, but they can be. It all depends on whether the method uses data outside of its own scope and parameter list or not.
For example, the following anonymous function would not be a closure, since it is self-contained and functionally equivalent to any other locally defined function:
System.Func<int,int> anon = (x) => { return x; };int result = anon(5); // result = 5
However, if the anonymous function pulled in data from outside itself, it becomes a closure, as it closes the environment around the required data. The following would result in a closure:
int i = 1024;System.Func<int,int> anon = (x) => { return x + i; };int result ...Read now
Unlock full access