By DuckMaestro


2011-03-25 21:27:31 8 Comments

I've seen countless posts on how variable capture pulls in variables for the creation of the closure, however they all seem to stop short of specific details and call the whole thing "compiler magic".

I'm looking for a clear-cut explanation of:

  1. How local variables are actually captured.
  2. The difference (if any) between capturing value types vs. reference types.
  3. And whether there is any boxing occurring with respect to value types.

My preference would be for an answer in terms of values and pointers (closer to the heart of what happens internally), though I will accept a clear answer involving values and references as well.

1 comments

@Jon Skeet 2011-03-25 21:30:38

  1. Is tricky. Will come onto it in a minute.
  2. There's no difference - in both cases, it's the variable itself which is captured.
  3. Nope, no boxing occurs.

It's probably easiest to demonstrate how the capturing works via an example...

Here's some code using a lambda expression which captures a single variable:

using System;

class Test
{
    static void Main()
    {
        Action action = CreateShowAndIncrementAction();
        action();
        action();
    }

    static Action CreateShowAndIncrementAction()
    {
        Random rng = new Random();
        int counter = rng.Next(10);
        Console.WriteLine("Initial value for counter: {0}", counter);
        return () =>
        {
            Console.WriteLine(counter);
            counter++;
        };
    }
}

Now here's what the compiler's doing for you - except that it would use "unspeakable" names which couldn't really occur in C#.

using System;

class Test
{
    static void Main()
    {
        Action action = CreateShowAndIncrementAction();
        action();
        action();
    }

    static Action CreateShowAndIncrementAction()
    {
        ActionHelper helper = new ActionHelper();        
        Random rng = new Random();
        helper.counter = rng.Next(10);
        Console.WriteLine("Initial value for counter: {0}", helper.counter);

        // Converts method group to a delegate, whose target will be a
        // reference to the instance of ActionHelper
        return helper.DoAction;
    }

    class ActionHelper
    {
        // Just for simplicity, make it public. I don't know if the
        // C# compiler really does.
        public int counter;

        public void DoAction()
        {
            Console.WriteLine(counter);
            counter++;
        }
    }
}

If you capture variables declared in a loop, you'd end up with a new instance of ActionHelper for each iteration of the loop - so you'd effectively capture different "instances" of the variables.

It gets more complicated when you capture variables from different scopes... let me know if you really want that sort of level of detail, or you could just write some code, decompile it in Reflector and follow it through :)

Note how:

  • There's no boxing involved
  • There are no pointers involved, or any other unsafe code

EDIT: Here's an example of two delegates sharing a variable. One delegate shows the current value of counter, the other increments it:

using System;

class Program
{
    static void Main(string[] args)
    {
        var tuple = CreateShowAndIncrementActions();
        var show = tuple.Item1;
        var increment = tuple.Item2;

        show(); // Prints 0
        show(); // Still prints 0
        increment();
        show(); // Now prints 1
    }

    static Tuple<Action, Action> CreateShowAndIncrementActions()
    {
        int counter = 0;
        Action show = () => { Console.WriteLine(counter); };
        Action increment = () => { counter++; };
        return Tuple.Create(show, increment);
    }
}

... and the expansion:

using System;

class Program
{
    static void Main(string[] args)
    {
        var tuple = CreateShowAndIncrementActions();
        var show = tuple.Item1;
        var increment = tuple.Item2;

        show(); // Prints 0
        show(); // Still prints 0
        increment();
        show(); // Now prints 1
    }

    static Tuple<Action, Action> CreateShowAndIncrementActions()
    {
        ActionHelper helper = new ActionHelper();
        helper.counter = 0;
        Action show = helper.Show;
        Action increment = helper.Increment;
        return Tuple.Create(show, increment);
    }

    class ActionHelper
    {
        public int counter;

        public void Show()
        {
            Console.WriteLine(counter);
        }

        public void Increment()
        {
            counter++;
        }
    }
}

@David Heffernan 2011-03-25 21:41:36

@Jon You say that the capture is of variables rather than values. This means, I guess, that if two lambdas, declared in the same method, refer to the same variable, then they both capture the same variable. This if one of the lambdas modifies that variable, then the other one sees the modified value held by that variable. Or am I wide of the mark?

@Jon Skeet 2011-03-25 21:42:42

@David: Yes, that's exactly right. In that case, there would be two instance methods in the generated class, and both delegates would reference the same target instance.

@David Heffernan 2011-03-25 21:46:48

@Jon Thanks. I don't actually know any C# and most of my time is spent with Delphi. The Delphi equivalent behaves the same way. It tends not to arise in most use cases, but I think most people's naive expectation is that values are captured. You can get an awful long way on that particular misunderstanding.

@Jon Skeet 2011-03-25 21:51:22

@David: Yes you can. Particularly as that's how anonymous classes work in Java :(

@David Heffernan 2011-03-25 21:52:55

@Jon There must be some cool example of someone creating two lambdas which share captured variables and mutually modifying them in a way to achieve something useful......

@DuckMaestro 2011-03-25 21:54:02

@Jon. I'm not sure I follow David's comment-question and your answer. If the captured variable is a value type, and you have two lambdas, won't those lambdas (by the example code above) take two separate copies of the value-type variable? such that if the first lambda later modifies that value, that the second lambda still has the value at the time of the copying?

@Jon Skeet 2011-03-25 21:57:27

@DuckMaestro: No, they won't. A single instance of the helper class will be created, and the two lambdas will both use that one instance to create delegates. Note how there isn't a counter variable in the CreateShowAndIncrementAction method in the expanded version - that variable only exists in the helper class. If two references refer to the same instance of that class, then a change via one will be seen by the other. There's no copying involved.

@Jon Skeet 2011-03-25 22:00:04

Example of David's scenario coming up... and now there.

@DuckMaestro 2011-03-25 22:04:46

@Jon. Thank you! That makes perfect sense.

@David Heffernan 2011-03-25 22:10:24

@Duck The key is to think of it as variable capture and not as value capture. I think once you have that clear, then it becomes easy to reason with.

@DuckMaestro 2011-03-25 22:43:31

Aye. In hindsight that now makes a lot more sense. Thanks.

@Brent 2013-08-23 05:52:06

What if the variable that was captured has gone out of scope? For example, the lambda is returned from a function.

@Jon Skeet 2013-08-23 05:59:19

@Brent: The variable still exists and can be used - the C# compiler effectively hoists it to another type, and makes sure that both the "immediate" code in the method and the code in the lambda expression use the same variable.

@davidbak 2014-06-28 20:00:04

@DavidHeffernan - (3 years later ...) See the example in section 3.1 of Structure and Interpretation of Computer Programs here where a bank account is implemented with two lambdas (withdraw and deposit) that share a captured account balance.

@Bart Juriewicz 2016-06-28 20:47:48

@JonSkeet, what happens when one of the captured variables is a class field, and one is not? Does the anonymous method point to that instance's field? Is this instance somehow passed to the generated class? And another question - is it possible, that the compiler doesn't know what anonymous methods share some captured variable?

@Jon Skeet 2016-06-28 20:49:20

@Bart: In that case, it's not the field that is captured, but the 'this' reference. If that's all that's captured, the compiler can just generate an instance method.

Related Questions

Sponsored Content

38 Answered Questions

[SOLVED] Static variables in JavaScript

11 Answered Questions

88 Answered Questions

[SOLVED] How do JavaScript closures work?

39 Answered Questions

[SOLVED] JavaScript closure inside loops – simple practical example

5 Answered Questions

[SOLVED] Is there a reason for C#'s reuse of the variable in a foreach?

12 Answered Questions

[SOLVED] JavaScript closures vs. anonymous functions

5 Answered Questions

[SOLVED] Curious null-coalescing operator custom implicit conversion behaviour

2 Answered Questions

[SOLVED] In closure, what triggers a new instance of the captured variable?

  • 2013-11-21 10:39:42
  • richard
  • 194 View
  • 5 Score
  • 2 Answer
  • Tags:   c# closures

7 Answered Questions

[SOLVED] Why are delegates reference types?

Sponsored Content