Anonymous c# delegate within a loop

前端 未结 3 824
无人共我
无人共我 2020-12-11 15:24

Hi all i am trying to write and anonymous delegate. as the integer variable is shared among the delegate i need it to be the local instance of every delegate such that rs[0]

3条回答
  •  星月不相逢
    2020-12-11 15:54

    Put int j = i inside your loop and refer to j within the lambda expression.

    If you are curious about why this happens, here is an MSDN blog entry containing a detailed technical explanation: Closing over the loop variable considered harmful

提交回复
热议问题