榨干委托那些知识点,能写出多少种委托(Action Func委托+异步委托)
以前写过一篇博文,是专门针对2.0委托的缘由和事件来说的,经过“水牛刀刀”的指点,抽出了1天时间,把3.5特性里的FCL自带的委托学习了下,总结给大家。
博文从3个方面来演示:
1.传统的委托的5种方式
2.Action和Func的委托
3.Action和Func的异步委托
如果对异步委托不太熟悉的朋友,建议先看看我的前几篇博文:
3.《委托与事件 在.net的争霸战 ,你选择了谁?(异步委托产生的原因)》
开始进入正题,学习就像是 爬山,每天努力一点,你都会离顶峰近一点。
1.传统的委托

delegate string Dele(int i);
class Program
{
static void Main(string[] args)
{
//委托初始化的5种方式
//方法1
Dele d1 = new Dele(Speak);
d1(10);
//方法2
Dele d2 = Speak;
d2(20);
//方法3
Dele d3=(int i1)=>Speak(i1);
d3(30);
//方法4
Dele d4 = (int i) => { string str = string.Format("The number is {0}", i); Console.WriteLine(str); return str; };
d4(40);
//方法5
Dele d5 = delegate(int i) { string str = string.Format("The number is {0}", i); Console.WriteLine(str); return str; };
d5(50);
}
static string Speak(int i)
{
string str = string.Format("The number is {0}",i);
Console.WriteLine(str);
return str;
}
}
输出:
The number is 10
The number is 20
The number is 30
The number is 40
The number is 50
2.Action委托
Action委托没有返回值的,而且Action<T>泛型委托最多是4个参数

class Program
{
static void Main(string[] args)
{
//Action委托初始化的5种方式
//方法1
Action<int> action1 = new Action<int>(Speak);
action1(10);
//方法2
Action<int> action2 = Speak;
action2(20);
//方法3
Action<int> action3=(int i1)=>Speak(i1);
action3(30);
//方法4
Action<int> action4 = (int i) => { string str = string.Format("The number is {0}", i); Console.WriteLine(str); };
action4(40);
//方法5
Action<int> action5 = delegate(int i) { string str = string.Format("The number is {0}", i); Console.WriteLine(str); };
action5(50);
}
static void Speak(int i)
{
string str = string.Format("The number is {0}",i);
Console.WriteLine(str);
}
}
输出:
The number is 10
The number is 20
The number is 30
The number is 40
The number is 50
注意:
1.Action没有返回值
2.Action非泛型委托,既没有参数也没有返回值
3.Func委托

class Program
{
static void Main(string[] args)
{
//Func委托初始化的5种方式
//方法1
Func<int, string> func1 = new Func<int, string>(Speak);
func1(10);
//方法2
Func<int, string> func2 = Speak;
func2(20);
//方法3
Func<int, string> func3=(int i1)=>Speak(i1);
func3(30);
//方法4
Func<int, string> func4= (int i) => { string str = string.Format("The number is {0}", i); Console.WriteLine(str); return str; };
func4(40);
//方法5
Func<int, string> func5 = delegate(int i) { string str = string.Format("The number is {0}", i); Console.WriteLine(str); return str; };
func5(50);
}
static string Speak(int i)
{
string str = string.Format("The number is {0}",i);
Console.WriteLine(str);
return str;
}
}
输出:
The number is 10
The number is 20
The number is 30
The number is 40
The number is 50
注:
1.Func可以无参数的,但是必须有返回值,即:无Func func;写法,只有Func<Tresult> func;写法
2.Func泛型委托,最多具有4个参数,1个返回值
.net 3.5的异步委托
1.Action的异步委托:
代码1:

class Program
{
static void Main(string[] args)
{
//Action委托的异步委托
Action<int> action = Speak;
AsyncCallback callback=new AsyncCallback(CallBackMethod);
action.BeginInvoke(10, callback, null);
Console.WriteLine("当前线程ID:{0}", Thread.CurrentThread.ManagedThreadId.ToString());
Console.ReadKey();
}
//回调方法
static void CallBackMethod(IAsyncResult ar)
{
AsyncResult result=(AsyncResult)ar;
Action<int> a = (Action<int>)result.AsyncDelegate;
a.EndInvoke(ar);
}
//被委托方法
static void Speak(int i)
{
string str = string.Format("The number is {0}",i);
Console.WriteLine("当前线程ID:{0}",Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(TimeSpan.FromSeconds(3));
Console.WriteLine(str);
}
}
注意:回调方法内部使用了IAsyncResult转换成AsyncResult类型,只是为了使用AsyncResult的属性AsyncDelegate,获取委托的对象,可以结束异步委托,同时取得返回值。
如下:

class Program
{
static void Main(string[] args)
{
//Action委托的异步委托
Action<int> action = Speak;
AsyncCallback callback=new AsyncCallback(CallBackMethod);
action.BeginInvoke(10, callback, action);//注意
Console.WriteLine("当前线程ID:{0}", Thread.CurrentThread.ManagedThreadId.ToString());
Console.ReadKey();
}
//回调方法
static void CallBackMethod(IAsyncResult ar)
{
Action<int> a = (Action<int>)ar.AsyncState;//注意
a.EndInvoke(ar);
}
//被委托方法
static void Speak(int i)
{
string str = string.Format("The number is {0}",i);
Console.WriteLine("当前线程ID:{0}",Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(TimeSpan.FromSeconds(3));
Console.WriteLine(str);
}
}
2.Func的异步委托
代码如下:

class Program
{
static void Main(string[] args)
{
//Action委托的异步委托
Func<int,string> func = Speak;
AsyncCallback callback=new AsyncCallback(CallBackMethod);
func.BeginInvoke(10, callback, func);//注意
Console.WriteLine("当前线程ID:{0}", Thread.CurrentThread.ManagedThreadId.ToString());
Console.ReadKey();
}
//回调方法
static void CallBackMethod(IAsyncResult ar)
{
Func<int, string> a = (Func<int, string>)ar.AsyncState;//注意
string str=a.EndInvoke(ar);
Console.WriteLine(str);//取得返回值
}
//被委托方法
static string Speak(int i)
{
string str = string.Format("The number is {0}",i);
Console.WriteLine("当前线程ID:{0}",Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(TimeSpan.FromSeconds(3));
return str;
}
总结:
本文代码居多,因为有些东西抽象,不容易说明白。有什么错误望大家指出来。
3.5新特性的FCL自带的委托,如果看完本文,你就可以理解了它有什么好处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库