Closure inside a delegate

suggest change

Closures are inline anonymous methods that have the ability to use Parent method variables and other anonymous methods which are defined in the parent’s scope.

In essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was first created - i.e. it can still use the local variables etc of the method which created it, even after that method has finished executing. – Jon Skeet
delegate int testDel();
static void Main(string[] args)
{
    int foo = 4;
    testDel myClosure = delegate()
    {
        return foo;
    };
    int bar = myClosure();

}

Example taken from Closures in .NET.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents