February 15, 2013

Closures in C#

Tricky topic to understand, this is is the simplest explanation I've found.

What?
A Closure is a function which can be passed around like an object but it also can maintain state in it's local variables.

Why?
Useful in asynchronous calls where you want something to happen when the task ends, the local variables can be used to store something that should be included in the work that needs to be done when the task completes.
Particularly useful in Javascript.