3

The testit() method is a closure. aString has fallen out of scope but testit() can still execute on it. testit2() is using a variable that hasn't fallen out of scope (mystring) but which was also not been passed into testit2(). Is testit2() considered a closure?

string mystring = "hello world";
Action testit = new Action(delegate { string aString = "in anon method"; Debug.WriteLine(aString); });
testit();

//capture mystring.  Is this still a closure?
Action testit2 = new Action(delegate { Debug.WriteLine(mystring); });
//mystring is still in scope
testit2();

In the second example, mystring can be updated outside of the method and those changes will be reflected in testit2(). This doesn't behave like a normal method, which would only be able to capture mystring as a parameter.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
4thSpace
  • 43,672
  • 97
  • 296
  • 475

3 Answers3

6

Both examples are anonymous functions. The second of which use a closure in order to capture the local variables. The scope lifetime of the variable with respect to the anonymous function does not affect whether or not a closure is created to use it. As long as the variable is defined outside a anonymous function and used within, a closure will be created.

The first anonymous function uses no local state, therefore does not need a closure. It should compile down to a static method.

This is necessary because the anonymous function can live past the lifetime of the current function. Therefore all locals used in a anonymous function must be captured in order to execute the delegate at a later time.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    They're anonymous methods, not lambda expressions (in C# terminology anyway). Both lambda expressions and anonymous methods count as anonymous functions, however. – Jon Skeet Feb 16 '09 at 19:51
  • I feel bad nit-picking terminology with a Softie :) – Jon Skeet Feb 16 '09 at 20:03
  • @Jon, nitpicks are welcome. Another chance to learn something new. I didn't know anonymous function was the universal term for these two different scenarios. I've taken to just calling them lambda's everywhere. Anonymous function is more difficult to type though ;) – JaredPar Feb 16 '09 at 20:07
3

A closure does not capture the values in scope, but the actual definition of the scope. So any other piece of code that has a reference to the same scope can modify variables within it.

krosenvold
  • 75,535
  • 32
  • 152
  • 208
1

testit isn't as much of a closure as testit2 is really - it's only using a locally defined variable, rather than one in the "parent" environment (such as mystring).

However, I'd argue that both are closures as they have the ability to capture variables from their enclosing environment, due to anonymous methods.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks. I was looking at your example from here: http://stackoverflow.com/questions/428617/closures-in-net. counter is defined just as testit() defines aString. I believe it means the example you gave isn't the strongest case for closures, which seems to be what you've stated for testit(). – 4thSpace Feb 16 '09 at 20:01
  • @4thspace: No, read the example again. counter is defined outside the anonymous method, more like mystring. – Jon Skeet Feb 16 '09 at 20:02