總結 委託和匿名委託的比較

2022-03-28 14:26:15 字數 1197 閱讀 6644

感覺匿名委託使用起來更簡潔一點,不用在定義乙個專用的委託函式來傳遞方法,也更可以更好的理解委託。

一、匿名委託

1

#region 匿名委託23

//定義委託

4delegate

string lookme(string

s);5

6protected

void linkbutton1_click(object

sender, eventargs e)7;

1011

//匿名委託呼叫

12string name1 = "

jarod";

13 label1.text =lm(name1);14}

1516

#endregion

二、實名委託

1

#region 委託的應用23

//委託的定義

4public

delegate

string delerate(string

year);56

//三個方法的定義,以供委託呼叫

7public

string year_2009(string

s)8 910

//三個方法的定義,以供委託呼叫

11public

string year_2008(string

s)12

1314

//三個方法的定義,以供委託呼叫

15public

string year_2007(string

s)16

1718

protected

void button1_click(object

sender, eventargs e)

1933

34//

執行委託的過程,把引數 year2 傳入實際執行的方法中去

35public

static

string getyear(delerate dr, string

year2)

36

3738

#endregion

**

C 委託 匿名委託

c 委託 匿名委託 委託變數在繫結方法時,是把方法名賦值給了委託變數名,這 樣就需要在類裡面定義方法,而有些方法只用到了 次,這樣的 方法在類裡面過多時,程式的可讀性就不是太好,針對這種情 況,可以使用匿名委託,即不用在類裡面額外定義方法.delegate定義的匿名委託 格式 delegate 引數...

匿名委託 為事件 event 註冊匿名委託

create a handler for a click event.button1.click delegate system.object o,system.eventargs e 註冊方法注意事項 1.操作符後 無 new 關鍵字 2.如需要 為 click 註冊匿名委託,則在 click 上...

匿名委託 為事件 event 註冊匿名委託

create a handler for a click event.button1.click delegate system.object o,system.eventargs e 註冊方法注意事項 1.操作符後 無 new 關鍵字 2.如需要 為 click 註冊匿名委託,則在 click 上...