I argumentend, because int is a value type, the actual value that is passed into Console.WriteLine() gets copied
That is exactly correct. When you call WriteLine
the value will be copied.
So, when are you calling WriteLine
? It's not in the for
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 variable i
is copied to the stack for the call to WriteLine
.
So, what's the value of i
during the foreach
loop? It's 10, for each iteration of the foreach
loop.
So now you're asking, "well how is i
anything during the foreach 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:
public class ClosureClass
{
public int i;
public void DoStuff()
{
Console.WriteLine(i);
}
}
class Program
{
delegate void Writer();
static void Main(string[] args)
{
var writers = new List<Writer>();
ClosureClass closure = new ClosureClass();
for (closure.i = 0; closure.i < 10; closure.i++)
{
writers.Add(closure.DoStuff);
}
foreach (Writer writer in writers)
{
writer();
}
}
}
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…