5.5.2 测试被捕获的变量的行为
string captured = "before x is created"; MethodInvoker x = delegate { Console.WriteLine(captured); captured = "changed by x"; }; Console.WriteLine(captured); captured = "directly before x is invoked"; x(); Console.WriteLine(captured); captured = "before second invocation"; x();
输出结果为
before x is created
directly before x is invoked
changed by x
before second invocation