【发布时间】:2010-11-05 10:15:07
【问题描述】:
有谁知道如何为 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; }
我将如何使用'delegate'类型来替换 Func 结构;我想弄清楚的原因是因为 Func 只能接受一个输入和一个返回变量。它不允许它所指向的方法具有设计灵活性。
谢谢!
【问题讨论】:
标签: c# .net multithreading delegates