/ Published in: C#

Shows an example of what a closure is.
Result: Counter=1 Counter= 2
Result: Counter=1 Counter= 2
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public class Test { static void Main() { Action action = Test.CreateAction(); action(); action(); Console.ReadLine(); } public static Action CreateAction() { int counter = 0; return delegate { // Yes, it could be done in one statement; // but it is clearer like this. counter++; Console.WriteLine("counter={0}", counter); }; } }
URL: http://csharpindepth.com/articles/chapter5/closures.aspx
Comments
