More Related Content
Similar to Andy On Closures
Similar to Andy On Closures (20)
More from melbournepatterns
More from melbournepatterns (20)
Andy On Closures
- 5. Example – C# class Program { delegate void Action(); static Action GetAction() { int x = 0; Action a = delegate { Console.WriteLine(x); }; x = 1; return a; } static void Main(string[] args) { Action a = GetAction(); a(); } }
- 6. Example – C# class Program { delegate void SomeDelegate(); public static void InvokeMethod () { SomeDelegate del = delegate() { Console.WriteLine("Hello2"); }; del(); } } class Program { delegate void SomeDelegate(); public static void InvokeMethod () { SomeDelegate del = new SomeDelegate( SomeMethod ); del(); } static void SomeMethod() { Console.WriteLine("Hello"); } } static void Main(string[] args) { InvokeMethod (); }