Mega Code Archive

 
Categories / C# / Language Basics
 

Anonymous methods can refer to local variables of the containing function and class members within the scope of the method

using System; public delegate void DelegateClass(out int arg); public class Starter {     public static void Main() {         DelegateClass del = MethodA();         int var;         del(out var);         Console.WriteLine(var);     }     public static DelegateClass MethodA() {         int local = 0;         return delegate(out int arg) {             arg = ++local;         };     } }