Essential C# Delegates  Suggest an edit

Closure inside a delegate

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.


Table Of Contents
22 Enum
25 GUID
95 Delegates
107 Stream
108 Timers
  ↑ ↓ to navigate     ↵ to select     Esc to close