loc*_*boy 1 .net c# multithreading delegates
有谁知道如何为Func和Action指针声明源代码?我试图理解使用委托进行异步调用背后的理论以及它如何与线程相关联.
例如,如果我有以下代码:
static void Main()
{
Func<string, int> method = Work;
IAsyncResult cookie = method.BeginInvoke ("test", null, null);
//
// ... here's where we can do other work in parallel...
//
int result = method.EndInvoke (cookie);
Console.WriteLine ("String length is: " + result);
}
static int Work (string s) { return s.Length; }
Run Code Online (Sandbox Code Playgroud)
我如何使用'委托'类型来替换Func <>结构; 我想弄明白的原因是因为Func只能输入一个输入和一个返回变量.它不允许设计灵活性指向它的方法.
谢谢!
Func<T>
没什么特别的,真的.它很简单:
public delegate T Func<T>();
Run Code Online (Sandbox Code Playgroud)
事实上,为了支持不同数量的参数,有一些声明,如:
public delegate void Action();
public delegate void Action<T>(T arg);
public delegate U Func<T, U>(T arg);
// so on...
Run Code Online (Sandbox Code Playgroud)