亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 C# 中的 lambda 表達式中刪除列表項?

如何從 C# 中的 lambda 表達式中刪除列表項?

C#
DIEA 2021-11-21 18:11:57
如果我有這個private static List<Action> actions = new List<Action>();private static void Main(){    for (var i = 0; i < 10; i += 1)    {        Action action = async () => {            // want to remove this specific lambda item from the "actions" variable.             // is there something like this:            /*                Action this_action = this;                actions.Remove(this_action);            */        };        actions.Add(action);    }    Parallel.Invoke(actions.ToArray());    Console.Write("\nPress any key to exit...");    Console.ReadKey();}如何從列表中正確刪除它。我需要某種自我參考?
查看完整描述

1 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

您可以僅使用 to 的引用action來刪除操作的實例。關閉將確保引用指向正確的對象。


for (var i = 0; i < 10; i += 1)

{

    Action action = null;

    action = () => {

        actions.Remove(action);

    };


    actions.Add(action);

}


Parallel.Invoke(actions.ToArray());

有關工作示例,請參閱Fiddle。


查看完整回答
反對 回復 2021-11-21
  • 1 回答
  • 0 關注
  • 500 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號