C#委託的介紹(delegate、Action、Func、predicate)

委託是一個類,它定義了方法的類型,使得可以將方法當作另一個方法的參數來進行傳遞。事件是一種特殊的委託。

1.委託的聲明

(1).delegate

delegate我們常用到的一種聲明

Delegate至少0個參數,至多32個參數,可以無返回值,也可以指定返回值類型。

例:publicdelegateint MethodtDelegate(int x, int y);表示有兩個參數,並返回int型。

(2).Action

Action是無返回值的泛型委託。

Action表示無參,無返回值的委託

Action<int,string>表示有傳入參數int,string無返回值的委託

Action<int,string,bool>表示有傳入參數int,string,bool無返回值的委託

Action<int,int,int,int> 表示有傳入4int型參數,無返回值的委託

Action至少0個參數,至多16個參數,無返回值。

例:

publicvoid Test<T>(Action<T> action,Tp)
{
action(p);
}

(3). Func

Func是有返回值的泛型委託

Func<int>表示無參,返回值爲int的委託

Func<object,string,int>表示傳入參數爲object, string 返回值爲int的委託

Func<object,string,int>表示傳入參數爲object, string 返回值爲int的委託

Func<T1,T2,,T3,int>表示傳入參數爲T1,T2,,T3(泛型)返回值爲int的委託

Func至少0個參數,至多16個參數,根據返回值泛型返回。必須有返回值,不可void

例:

publicint Test<T1,T2>(Func<T1,T2,int>func,T1 a,T2 b)
{
return func(a, b);
}

(4) .predicate

predicate是返回bool型的泛型委託

predicate<int>表示傳入參數爲int 返回bool的委託

Predicate有且只有一個參數,返回值固定爲bool

例:publicdelegatebool Predicate<T> (T obj)

2.委託的使用

(1).Delegate的使用


publicdelegateintMethodDelegate(int x, int y);
privatestatic MethodDelegatemethod;
staticvoid Main(string[] args)
{
method = newMethodDelegate(Add);
Console.WriteLine(method(10,20));
Console.ReadKey();
}

privatestaticint Add(int x, int y)
{
return x + y;
}


(2).Action的使用


staticvoid Main(string[] args)
{
Test<
string>(Action,"Hello World!");
Test<
int>(Action, 1000);
Test<
string>(p => { Console.WriteLine("{0}", p); }, "Hello World");//使用Lambda表達式定義委託
Console.ReadKey();
}
public
staticvoidTest<T>(Action<T> action, T p)
{
action(p);
}
private
staticvoid Action(string s)
{
Console.WriteLine(s);
}
privatestaticvoid Action(int s)
{
Console.WriteLine(s);
}

可以使用Action<T1, T2, T3, T4> 委託以參數形式傳遞方法,而不用顯式聲明自定義的委託。 封裝的方法必須與此委託定義的方法簽名相對應。也就是說,封裝的方法必須具有四個均通過值傳遞給它的參數,並且不能返回值。 (在 C# 中,該方法必須返回 void)通常,這種方法用於執行某個操作。

(3).Func的使用


staticvoid Main(string[] args)
{
Console.WriteLine(Test<int,int>(Fun,100,200));
Console.ReadKey();
}
publicstaticintTest<T1, T2>(Func<T1, T2, int>func, T1 a, T2 b)
{
return func(a, b);
}
private staticint Fun(int a, int b)
{
return a + b;
}


(4). predicate的使用

泛型委託:表示定義一組條件並確定指定對象是否符合這些條件的方法。此委託由Array 和 List 類的幾種方法使用,用於在集合中搜索元素。


staticvoid Main(string[] args)
{
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };
Point first =Array.Find(points, ProductGT10);
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
Console.ReadKey();
}
privatestaticbool ProductGT10(Point p)
{
if (p.X * p.Y > 100000)
{
returntrue;
}
else
{
returnfalse;
}
}


使用帶有Array.Find 方法的 Predicate 委託搜索 Point 結構的數組。如果 X 和 Y 字段的乘積大於 100,000,此委託表示的方法ProductGT10 將返回 true。Find 方法爲數組的每個元素調用此委託,在符合測試條件的第一個點處停止。

3.委託的清空

(1).在類中申明清空委託方法,依次循環去除委託引用。

方法如下:


public MethodDelegate OnDelegate;
publicvoidClearDelegate()
{
while (this.OnDelegate!= null)
{
this.OnDelegate -= this.OnDelegate;
}
}


(2).如果在類中沒有申明清空委託的方法,我們可以利用GetInvocationList查詢出委託引用,然後進行去除。

方法如下:


public MethodDelegate OnDelegate;

staticvoid Main(string[] args)
{
Program test = new Program();

if (test.OnDelegate != null)
{
System.Delegate[] dels =test.OnDelegate.GetInvocationList();
for (int i = 0; i < dels.Length; i++)
{
test.OnDelegate -=dels[i] asMethodDelegate;
}
}
}


4.委託的特點

委託類似於C++ 函數指針,但它們是類型安全的。

委託允許將方法作爲參數進行傳遞。

委託可用於定義回調方法。

委託可以鏈接在一起;例如,可以對一個事件調用多個方法。

方法不必與委託簽名完全匹配。

5.總結:

Delegate至少0個參數,至多32個參數,可以無返回值,也可以指定返回值類型

Func可以接受0個至4個傳入參數,必須具有返回值

Action可以接受0個至4個傳入參數,無返回值

Predicate只能接受一個傳入參數,返回值爲bool類型


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章