Local variables with Delegates

11,793

Solution 1

currentValue is no longer a local variable: it is a captured variable. This compiles to something like:

class Foo {
  public string currentValue; // yes, it is a field

  public void SomeMethod(object sender, EventArgs e) {
    Response.Write(currentValue);
  }
}
...
public Page_Index() {
  Foo foo = new Foo();
  foo.currentValue = "This is the FIRST value";
  this.Load += foo.SomeMethod;

  foo.currentValue = "This is the MODIFIED value";
}

Jon Skeet has a really good write up of this in C# in Depth, and a separate (not as detailed) discussion here.

Note that the variable currentValue is now on the heap, not the stack - this has lots of implications, not least that it can now be used by various callers.

This is different to java: in java the value of a variable is captured. In C#, the variable itself is captured.

Solution 2

I suppose more the question I am asking is that how is it working with a local variable [MG edit: "Ack - ignore this..." was added afterwards]

That is the point; it really isn't a local variable any more - at least, not in terms of how we normally think of them (on the stack etc). It looks like one, but it isn't.

And for info, re "not good practice" - anonymous methods and captured variables are actually an incredibly powerful tool, especially when working with events. Feel free to use them, but if you are going down this route, I would recommend picking up Jon's book to make sure you understand what is actually happening.

Share:
11,793

Related videos on Youtube

user1709101
Author by

user1709101

Designer, developer, geek-dad

Updated on April 17, 2022

Comments

  • user1709101
    user1709101 about 2 years

    This is clearly not appears like it wouldn't be a best practice. Can someone explain why it would not be a best practice or how this works? Any books or articles providing an explanation would be appreciated.

    //The constructor
    public Page_Index() {
    
        //create a local value
        string currentValue = "This is the FIRST value";
    
        //use the local variable in a delegate that fires later
        this.Load += delegate(object sender, EventArgs e) {
            Response.Write(currentValue);
        };
    
        //change it again
        currentValue = "This is the MODIFIED value";
    
    }
    

    The value that is output is the second value "Modified". What part of the compiler magic is making this work? Is this as simple as keeping track of the value on the heap and retrieving it again later?

    [Edit]: Given some of the comments, changing the original sentence some...

  • Jon Skeet
    Jon Skeet over 15 years
    As well as writing it up in C# in Depth (thanks for the plug!), I've got an article comparing C# and Java closures and explaining why they're nice: csharpindepth.com/Articles/Chapter5/Closures.aspx
  • Marc Gravell
    Marc Gravell over 15 years
    Ah - as you were writing that comment, I was editing the above to include it. Double the value ;-p
  • chiccodoro
    chiccodoro over 13 years
    @Mark: To slightly improve your great answer, could you exchange " variable value " by " value of the variable " in your last sentence? Since the variable is called "currentValue" it can confuse the reader (like me), first thinking that the italic " value " refers to the name of the variable (" which variable? The one called value ")
  • Prashant Cholachagudda
    Prashant Cholachagudda over 13 years
    I recommend Eric Lippert's recent blog post, about how variables stored and managed. blogs.msdn.com/b/ericlippert/archive/2010/09/30/…