MethodInvoke委托或lambda表达式

Jac*_*ack 14 c# lambda delegates invoke method-invocation

两者有什么区别?

Invoke((MethodInvoker) delegate {
        checkedListBox1.Items.RemoveAt(i);
        checkedListBox1.Items.Insert(i, temp + validity);
        checkedListBox1.Update();
    }
);
Run Code Online (Sandbox Code Playgroud)

VS

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

有没有理由使用lambda表达式?并将(MethodInvoker)委托和lambda转换为MethodInvoker类型?什么样的表达不需要(MethodInvoker)演员?

小智 20

1)lambda表达式更短更清晰

2)是的

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

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