加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – MethodInvoke委托或lambda表达式

发布时间:2020-12-15 17:37:04 所属栏目:百科 来源:网络整理
导读:两者有什么区别? Invoke((MethodInvoker) delegate { checkedListBox1.Items.RemoveAt(i); checkedListBox1.Items.Insert(i,temp + validity); checkedListBox1.Update(); }); VS Invoke((MethodInvoker) ( () = { checkedListBox1.Items.RemoveAt(i); chec
两者有什么区别?
Invoke((MethodInvoker) delegate {
        checkedListBox1.Items.RemoveAt(i);
        checkedListBox1.Items.Insert(i,temp + validity);
        checkedListBox1.Update();
    }
);

VS

Invoke((MethodInvoker)
    (
        () => 
        {
            checkedListBox1.Items.RemoveAt(i);
            checkedListBox1.Items.Insert(i,temp + validity);
            checkedListBox1.Update();
        }
    )
);

有没有理由使用lambda表达式?和(MethodInvoker)将代理和lambda转换成MethodInvoker类型?什么样的表达式不需要(MethodInvoker)转换?

解决方法

1)lambda表达式更短,更干净

2)是的

3)您可以使用Action类型,如下所示:

Invoke(new Action(
    () => 
    {
        checkedListBox1.Items.RemoveAt(i);
        checkedListBox1.Items.Insert(i,temp + validity);
        checkedListBox1.Update();
    }
)
);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读