views:

196

answers:

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.

+5  A: 

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
They're anonymous methods, not lambda expressions (in C# terminology anyway). Both lambda expressions and anonymous methods count as anonymous functions, however.
Jon Skeet
@Jon, thanks, updated the terminology
JaredPar
I feel bad nit-picking terminology with a Softie :)
Jon Skeet
@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
+2  A: 

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
This kind of closure is known as a "lexical closure" - it captures the lexical scope.
Justice
A: 

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
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
@4thspace: No, read the example again. counter is defined outside the anonymous method, more like mystring.
Jon Skeet