c# - C# 中的调用和委托(delegate)

标签 c# winforms delegates invoke

有人可以解释这段代码中的语法吗?

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

我正在使用需要更新部分 UI 的后台工作程序,所以我使用了它。它有效,但我不知道空的 () 和 => 是什么意思。

最佳答案

() 和 => 是一个 lambda expression .

Action a = () => { 
    //code here
}

Action 类型的委托(delegate),它执行 block 中的代码。

Func<string> f = () => {
    //code here
    return "string";
}

Func<string> 类型的委托(delegate),它执行 block 中的代码,然后返回一个字符串。

Func<int, int, string> f = (i, j) => {
    //code here
    return "string"+i+j;
}

Func<int, int, string> 类型的委托(delegate),它有两个 int 参数,在代码块中引用 i 和 j,并返回一个字符串。

等等...

关于c# - C# 中的调用和委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7552103/

相关文章:

c# - 如何将参数传递给C#类?

c# - 因为它有一个 DefiningQuery 并且 <ModificationFunctionMapping> 元素中不存在 <InsertFunction> 元素

c# - 这行 C# 代码实际上做了什么?

c++ - 委托(delegate)构造函数不能有其他内存初始化器 - 错误

ios - 未调用 Swift 3 委托(delegate)函数

c# - 无法通过 JavaScript 清除 ASP.net

c# - 按相等分组对象

c# - 如何使用 C# 启动数据库事务,在 MySQL 上运行多个查询然后提交或回滚?

c# - 在 ErrorProvider 中填充顶部和底部

c# - DataGridView控件如何实现级联ComboBox?