以前写过一篇博文,是专门针对2.0委托的缘由和事件来说的,经过“水牛刀刀”的指点,抽出了1天时间,把3.5特性里的FCL自带的委托学习了下,总结给大家。
博文从3个方面来演示:
1.传统的委托的5种方式
2.Action和Func的委托
3.Action和Func的异步委托
如果对异步委托不太熟悉的朋友,建议先看看我的前几篇博文:
3.《委托与事件 在.net的争霸战 ,你选择了谁?(异步委托产生的原因)》
开始进入正题,学习就像是 爬山,每天努力一点,你都会离顶峰近一点。
1.传统的委托
View Code
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个参数
View Code
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委托
View Code
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:
View Code
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,获取委托的对象,可以结束异步委托,同时取得返回值。
如下:
View Code
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的异步委托
代码如下:
View Code
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自带的委托,如果看完本文,你就可以理解了它有什么好处。