Hi,
In the .NET 2.0 Beta 2 documentation, the following is wrtten about
anonymous methods:
Unlike local variables, the lifetime of the outer variable extends until the
delegates that reference the anonymous methods are eligible to garbage
collection. The value of n is captured at the time the delegate is created.
The example shows an int that is used in the anonymous method block. That
int then becomes an outer variable it's value is captured at the creation of
the anonymous code block.
That's good with value type. But what about reference type? How is the value
"captured"?
Let's change the example.
int n = 0;
MyDel d = new MyDel() { Console.WriteLine("Copy #:{0}", ++n); };
To
MyClass n = new MyClass();
MyDel d = new MyDel() { Console.WriteLine("Copy #:{0}", n.SomeProperty); };
How is the value of n captured since it's a reference type? Will it compiles
at all?
Etienne Fortin