有人可以解释这段代码中的语法吗?
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/