Contents tagged with Closures

  • How closures are implemented by .Net

    Hello everybody,

    today I want to share piece of wisdom which is interesting, but hard to explain why somebody may need it. That is how closures are implemented. 

    Consider the following code:

    Snippet

    class Program

        {

            static void Main(string[] args)

            {

                int a = 54;

                Task t = new Task(

                    () =>

                    {

                        a++;

                        Console.WriteLine("Inside task");

                    }

                    );

                t.Start();

            }

        }

    Now you may wonder, how a will be passed to closure? How Console.WriteLine will be executed?

    For my surprise .Net for closures generates behind the … more