I was attending the TechDays 2013 in the Netherlands this week and I got an interesting quiz question presented. The question was: What is the output of the following program. Here is what the code looks like.
class Program
{
delegate void Writer();
static void Main(string[] args)
{
var writers = new List<Writer>();
for (int i = 0; i < 10; i++)
{
writers.Add(delegate { Console.WriteLine(i); });
}
foreach (Writer writer in writers)
{
writer();
}
}
}
Obviously, the answer I gave was wrong. I argumentend, because int is a value type, the actual value that is passed into Console.WriteLine()
gets copied, so the output would be 0...9. However i
is handled as a reference type in this situation. The correct answer is that it will display ten times 10. Can anyone explain why and how?
It is because it is a captured variable. Note that this used to also happen with
foreach
, but that changed in C# 5. But to re-write your code to what you actually have:As you can see: there is only one
ctx
thus only onectx.i
, and it is 10 by the time youforeach
overwriters
.Btw, if you want to make the old code work:
Basically, the capture-context is scoped at the same level as the variable; here the variable is scoped inside the loop, so this generates:
Here each
DoStuff
is on a different capture-context instance, so has a different and separatei
.That is exactly correct. When you call
WriteLine
the value will be copied.So, when are you calling
WriteLine
? It's not in thefor
loop. You're not writing anything at that point in time, you're just creating a delegate.It's not until the
foreach
loop when you invoke the delegate, it's at that time that the value in the variablei
is copied to the stack for the call toWriteLine
.So, what's the value of
i
during theforeach
loop? It's 10, for each iteration of theforeach
loop.So now you're asking, "well how is
i
anything during theforeach loop, isn't it out of scope
. Well, no, it's not. What this is demonstrating is a "closure". When an anonymous method reference a variable that variable's scope needs to last for as long as that anonymous method, which could be for any period of time. If nothing special is done at all reading the variable would be random garbage containing whatever happened to be stuck in that location in memory. C# actively makes sure that situation can't happen.So what does it do? It creates a closure class; it's a class that will contain a number of fields representing everything that is closed over. In other words, the code will be refactored to look something like this:
Now we both have a name for our anonymous method (all anonymous methods are given a name by the compiler) and we can ensure that the variable will live for as long as the delegate that refers to the anonymous function lives.
Looking at this refactor, I hope it's clear why the result is that
10
is printed 10 times.In your case, the delegated methods are anonymous methods accessing a local variable (the for loop index
i
). That is, these are clousures.Since the anonymous method is called ten times after the for loop, it gets the most recent value for i.
A simple sample of various clousures accessing the same reference
Here's a simplified version of clousure behavior:
Check this other Q&A (What are clousures in .NET?) on StackOverflow for more info about what are C#/.NET clousures!
For me, it's easier to understand by comparing the old behavior and the new behavior with the native
Action
class in place of a customWriter
.Before C# 5 closure captured the same variable (not the value of the variable) in cases of for, foreach variables and local variable captures. So given the code:
We see just the last value that we set for the variable
forLoopVariable
. However, with C# 5, the foreach loop has been modified. Now we capture distinct variables.E.G.
So the output is more intuitive: 0,1,2...
Note that this is a breaking change (albeit its assumed to be a minor one). And that may be why the for loop behavior remains unchanged with C# 5.