如何告诉lambda函数捕获副本而不是C#中的引用?

我一直在学习C#,我正在努力理解lambdas。在下面的示例中,它打印出10次。


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变量的引用或指针,并且每次循环迭代时都会为同一个引用分配一个新值。有没有办法强制lamda获取副本,如C ++ 0x语法


[&](){ ... } // Capture by reference


[=](){ ... } // Capture copies


炎炎设计
浏览 503回答 3
3回答

幕布斯7119047

编译器正在做的是将lambda和lambda捕获的任何变量拉入编译器生成的嵌套类中。编译后,您的示例看起来很像这样:class Program{&nbsp; &nbsp; &nbsp; &nbsp; delegate void Action();&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Action> actions = new List<Action>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DisplayClass1 displayClass1 = new DisplayClass1();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (displayClass1.i = 0; displayClass1.i < 10; ++displayClass1.i )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actions.Add(new Action(displayClass1.Lambda));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (Action a in actions)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; class DisplayClass1&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; void Lambda()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }}通过在for循环中创建副本,编译器在每次迭代中生成新对象,如下所示:for (int i = 0; i < 10; ++i){&nbsp; &nbsp; DisplayClass1 displayClass1 = new DisplayClass1();&nbsp; &nbsp; displayClass1.i = i;&nbsp; &nbsp; actions.Add(new Action(displayClass1.Lambda));}

呼如林

我能找到的唯一解决方案是首先制作本地副本:for (int i = 0; i < 10; ++i){&nbsp; &nbsp; int copy = i;&nbsp; &nbsp; actions.Add(() => Console.WriteLine(copy));}但是我无法理解为什么在for-loop中放置副本与使用lambda捕获有什么不同i。
打开App,查看更多内容
随时随地看视频慕课网APP