以前寫過一篇博文,是專門針對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自帶的委托,如果看完本文,你就可以理解了它有什么好處。