C#委托用法詳解
1、什么是委托
從數(shù)據(jù)結構來講,委托是和類一樣是一種用戶自定義類型。
委托是方法的抽象,它存儲的就是一系列具有相同簽名和返回回類型的方法的地址。調用委托的時候,委托包含的所有方法將被執(zhí)行。
2、委托的定義
委托是類型,就好像類是類型一樣。與類一樣,委托類型必須在被用來創(chuàng)建變量以及類型對象之前聲明。
委托的聲明原型是
delegate <函數(shù)返回類型> <委托名> (<函數(shù)參數(shù)>)
例子:public delegate void MyDelegate(int number);//定義了一個委托MyDelegate,它可以注冊返回void類型且有一個int作為參數(shù)的函數(shù)
3、委托的實例化
3.1 使用new關鍵字
委托實例化的原型是
<委托類型> <實例化名>=new <委托類型>(<注冊函數(shù)>)
例子:MyDelegate _MyDelegate=new MyDelegate(CheckMod);//用函數(shù)CheckMod實例化上面的MyDelegate 委托為_MyDelegate
3.2 使用匿名方法
<委托類型> <實例化名>=delegate(<函數(shù)參數(shù)>){函數(shù)體};
3.3 使用Lambda表達式
class Program { //聲明委托 delegate int MyDelegate(int x, int y); static void Main(string[] args) { //實例化委托 //1、使用new關鍵字 MyDelegate _myDelegate = new MyDelegate(GetSum); //2、使用匿名方法 MyDelegate myDelegate = delegate(int x, int y) { return x + y; }; //3、使用Lambda表達式 MyDelegate myDelegateLambda = (int x, int y) => { return x + y; }; } static int GetSum(int x, int y) { return x + y; } }
4、泛型委托
委托也支持泛型的使用
泛型委托原型:
delegate <T1> <委托名><T1,T2,T3...> (T1 t1,T2 t2,T3 t3...)
例如:delegate T2 DelegateDemo<T1,T2>(T1 t);//定義有兩個泛型(T1,T2)的委托,T2作為委托函數(shù)返回類型,T1作為委托函數(shù)參數(shù)類型
static boo Check(int i) { if(i%2==0) { return true; } return false; } static void Main(string[] args) { DelegateDemo<int, bool> _delegate =Check;//將泛型委托委托<T1,T2>實例化為<int,bool>,即表示有一個int類型參數(shù)且返回類型是bool的函數(shù). Console.WriteLine(_delegate(9));//false }
5、C#內(nèi)置泛型委托
C#共有3種內(nèi)置泛型委托
namespace DelegateDemo { class Program { //聲明委托 delegate int MyDelegate(int x, int y); static void Main(string[] args) { //1、Action<T>只能委托必須是無返回值的方法 Action<string> _action = new Action<string>(SayHello); _action("Hello World"); //2、Fun<TResult>只是委托必須有返回值的方法 Func<int, bool> _func = new Func<int, bool>(Check); _func(5); //3、Predicate:此委托返回一個bool值,該委托通常引用一個"判斷條件函數(shù)"。 //需要指出的是,判斷條件一般為“外部的硬性條件”,比如“大于50”,而不是由數(shù)據(jù)自身指定,如“查找數(shù)組中最大的元素就不適合”。 Predicate<int> _predicate = new Predicate<int>(Check); //使用Lambda表達式 Predicate<int> predicate = p => p % 2 == 0; _predicate(26); } static void SayHello(string strMsg) { Console.WriteLine(strMsg); } //返回值為bool值 static bool Check(int i) { if (i % 2 == 0) { return true; } return false; } } }
6、多播委托
實例化委托時必須將一個匹配函數(shù)注冊到委托上來實例化一個委托對象,但是一個實例化委托不僅可以注冊一個函數(shù)還可以注冊多個函數(shù),注冊多個函數(shù)后,在執(zhí)行委托的時候會根據(jù)注冊函數(shù)的注冊先后順序依次執(zhí)行每一個注冊函數(shù)。
函數(shù)注冊委托的原型:
<委托類型> <實例化名>+=new <委托類型>(<注冊函數(shù)>)
例如:MyDelegate _myDelegate+=new MyDelegate(CheckMod);//將函數(shù)CheckMod注冊到委托實例_checkDelegate上
在.net 2.0開始可以直接將匹配的函數(shù)注冊到實例化委托:
<委托類型> <實例化名>+=<注冊函數(shù)>
例如:MyDelegate _myDelegate+=CheckMod;//將函數(shù)CheckMod注冊到委托實例_myDelegate上
注意:委托必須先實例化以后,才能使用+=注冊其他方法。如果對注冊了函數(shù)的委托實例從新使用=號賦值,相當于是重新實例化了委托,之前在上面注冊的函數(shù)和委托實例之間也不再產(chǎn)生任何關系。
有+=注冊函數(shù)到委托,也有-=解除注冊
例如:MyDelegate _myDelegate-=CheckMod;
如果在委托注冊了多個函數(shù)后,如果委托有返回值,那么調用委托時,返回的將是最后一個注冊函數(shù)的返回值。
namespace DelegateDemo { class Program { //聲明委托 delegate int MyDelegate(int x, int y); static void Main(string[] args) { MyDelegate _myDelegate = new MyDelegate(fun1); _myDelegate += fun2; Console.WriteLine(_myDelegate(10,23)); Console.ReadKey();//輸出10,返回最后一個注冊函數(shù)的返回值 } static int fun1(int x, int y) { return x + y; } static int fun2(int x, int y) { return x; } } }
到此這篇關于C#委托用法的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#使用HtmlAgilityPack抓取糗事百科內(nèi)容實例
這篇文章主要介紹了C#使用HtmlAgilityPack抓取糗事百科內(nèi)容的方法,實例分析了C#中HtmlAgilityPack的相關使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07