内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
正在试着理解Lambdas
class Program { delegate void Action(); static void Main(string[] args) { List<Action> actions = new List<Action>(); for (int i = 0; i < 10; ++i ) actions.Add(()=>Console.WriteLine(i)); foreach (Action a in actions) a(); } }
显然,在lambda后面生成的类正在存储指向int i
变量
[&](){ ... } // Capture by reference
[=](){ ... } // Capture copies