guh*_*hou 5 c# syntax lambda delegates
这更像是一个C#语法问题,而不是一个需要解决的实际问题.假设我有一个将委托作为参数的方法.假设我定义了以下方法:
void TakeSomeDelegates(Action<int> action, Func<float, Foo, Bar, string> func)
{
// Do something exciting
}
void FirstAction(int arg) { /* something */ }
string SecondFunc(float one, Foo two, Bar three){ /* etc */ }
Run Code Online (Sandbox Code Playgroud)
现在,如果我想TakeSomeDelegates
用FirstAction
和SecondFunc
作为参数调用,据我所知,我需要做这样的事情:
TakeSomeDelegates(x => FirstAction(x), (x,y,z) => SecondFunc(x,y,z));
Run Code Online (Sandbox Code Playgroud)
但是有没有更方便的方法来使用适合所需委托签名的方法而无需编写lambda?理想情况下TakeSomeDelegates(FirstAction, SecondFunc)
,虽然显然不能编译.
您正在寻找的是一种称为“方法组”的东西。使用这些,您可以替换一行 lamda,例如:
曾是:
TakeSomeDelegates(x => firstAction(x), (x, y, z) => secondFunc(x, y, z));
Run Code Online (Sandbox Code Playgroud)
替换为方法组后:
TakeSomeDelegates(firstAction, secondFunc);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
181 次 |
最近记录: |