C# Func<&燃气轮机;和原始代码
有人知道Func和操作指针的源代码是如何声明的吗?我试图理解使用委托进行异步调用背后的理论,以及它与线程的关系 例如,如果我有以下代码:C# Func<&燃气轮机;和原始代码,c#,.net,multithreading,delegates,C#,.net,Multithreading,Delegates,有人知道Func和操作指针的源代码是如何声明的吗?我试图理解使用委托进行异步调用背后的理论,以及它与线程的关系 例如,如果我有以下代码: static void Main() { Func<string, int> method = Work; IAsyncResult cookie = method.BeginInvoke ("test", null, null); // // ... here's where we can do other work in
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; }
static void Main()
{
Func方法=工作;
IAsyncResult cookie=method.BeginInvoke(“测试”,null,null);
//
//…在这里我们可以同时做其他工作。。。
//
int result=method.EndInvoke(cookie);
Console.WriteLine(“字符串长度为:”+result);
}
静态整型工作(字符串s){返回s.Length;}
如何使用“委托”类型替换Func结构;我想弄明白的原因是Func只能接受一个输入和一个返回变量。它不允许它所指向的方法具有设计灵活性
谢谢 Func
真的没什么特别的。很简单:
public delegate T Func<T>();
公共委托T Func();
事实上,为了支持不同数量的参数,声明了一系列参数,如:
public delegate void Action();
public delegate void Action<T>(T arg);
public delegate U Func<T, U>(T arg);
// so on...
公共委托无效操作();
公共代表无效行动(T参数);
公共代表U职能(T参数);
//等等。。。
Func
真的没什么特别的。很简单:
public delegate T Func<T>();
公共委托T Func();
事实上,为了支持不同数量的参数,声明了一系列参数,如:
public delegate void Action();
public delegate void Action<T>(T arg);
public delegate U Func<T, U>(T arg);
// so on...
公共委托无效操作();
公共代表无效行动(T参数);
公共代表U职能(T参数);
//等等。。。
Func
只是一个普通委托。它只是帮助您避免编写常见的委托。就这样。如果它不适合你,你应该写你自己的delagate。
取代你要的那件的德拉加特是
delegate string Method(int parm);
如果您想要一个接受22:-)整数并返回字符串的func(表示istance),则必须编写自己的委托
delegate string CrazyMethod(int parm1,int parm2,.....)
就你而言
delegate int MyOwnDeletage(string d);
class Program
{
static int Work(string s) { return s.Length; }
static void Main(string[] args)
{
// Func<string, int> method = Work;
MyOwnDeletage 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);
}
}
delegate int MyOwnDeletage(字符串d);
班级计划
{
静态整型工作(字符串s){返回s.Length;}
静态void Main(字符串[]参数)
{
//Func方法=工作;
MyOwnDeletage方法=工作;
IAsyncResult cookie=method.BeginInvoke(“测试”,null,null);
//
//…在这里我们可以同时做其他工作。。。
//
int result=method.EndInvoke(cookie);
Console.WriteLine(“字符串长度为:”+result);
}
}
Func
只是一个普通委托。它只是帮助您避免编写常见的委托。就这样。如果它不适合你,你应该写你自己的delagate。
取代你要的那件的德拉加特是
delegate string Method(int parm);
如果您想要一个接受22:-)整数并返回字符串的func(表示istance),则必须编写自己的委托
delegate string CrazyMethod(int parm1,int parm2,.....)
就你而言
delegate int MyOwnDeletage(string d);
class Program
{
static int Work(string s) { return s.Length; }
static void Main(string[] args)
{
// Func<string, int> method = Work;
MyOwnDeletage 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);
}
}
delegate int MyOwnDeletage(字符串d);
班级计划
{
静态整型工作(字符串s){返回s.Length;}
静态void Main(字符串[]参数)
{
//Func方法=工作;
MyOwnDeletage方法=工作;
IAsyncResult cookie=method.BeginInvoke(“测试”,null,null);
//
//…在这里我们可以同时做其他工作。。。
//
int result=method.EndInvoke(cookie);
Console.WriteLine(“字符串长度为:”+result);
}
}
Small point-OP正在使用:delegate TResult Func(T arg)对不起,我有点迷路了。你能在声明中给我更多的信息吗?当我说“Func method=Work;”时,如果你能在代码方面帮助我,也许会更清楚。我怎样才能使用“delegate”关键字替换该段?@cfarm54:我可能没有正确理解你的问题。如果您想知道定义匿名方法的语法,应该是:Func method=delegate(string s){return s.Length;}代码>。当然,您可以使用lambda语法并更加简洁:Func method=s=>s.Length代码>。必须指出的是,匿名方法绝不限于Func
和Action
委托。他们可以处理与其签名匹配的任何委托类型对不起,我有点迷路了。你能在声明中给我更多的信息吗?当我说“Func method=Work;”时,如果你能在代码方面帮助我,也许会更清楚。我怎样才能使用“delegate”关键字替换该段?@cfarm54:我可能没有正确理解你的问题。如果您想知道定义匿名方法的语法,应该是:Func method=delegate(string s){return s.Length;}代码>。当然,您可以使用lambda语法并更加简洁:Func method=s=>s.Length代码>。必须指出的是,匿名方法绝不限于Func
和Action
委托。他们可以处理与签名匹配的任何委托类型。我如何在上面的代码中替换此实例化,以便调用beginInvoke()方法并将其指向work()方法?请看我的帖子我已经编辑过了。我可以在这里回答,因为我必须发布代码谢谢,这是我一直在寻找的答案。我将如何在上面的代码中替换此实例化,以便调用beginInvoke()方法并将其指向Work()方法?看看我的帖子,我已经编辑了它。我可以在这里回答,因为我必须发布代码谢谢,这是我一直在寻找的答案