Closure

Example: using System; public class ClosureTest {

public static void Main { new ClosureTest; }

int x = 7;

ClosureTest { var f1 = ClosureTest1; // var c1 = new ClosureTest1Class; c1.y = x;       var f2 = ClosureTest2; // var c2 = new ClosureTest2Class; c2._this = this; var f3 = ClosureTest3; // var c3 = new ClosureTest3Class; c3._this = this;

x = 12;

Console.WriteLine(f1.ToString); // 7 Console.WriteLine(f2.ToString); // 12 Console.WriteLine(f3.ToString); // 12 }

/*   class ClosureTest1Class { public int y;       public int func { return y;       } }   class ClosureTest2Class { public ClosureTest _this; public int func { int y =_this.x;           return y;        } }   class ClosureTest3Class { public ClosureTest _this; public int func { return _this.x; } }   */

Func ClosureTest1 { int y = x;       return  => { return y;       }; }

Func ClosureTest2 { return => { int y = x;           return y;        }; }

Func ClosureTest3 { return => { return x;       }; } }