c#中的泛型委托詳解
今天學(xué)習(xí)一下c#中的泛型委托。
1.一般的委托,delegate,可以又傳入?yún)?shù)(<=32),聲明的方法為 public delegate void SomethingDelegate(int a);
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace delegateSummary { public delegate void GetIntDelegate(int a); //聲明一個委托 public class getIntClass { public static void SetDelegateString(int a,GetIntDelegate getIntDelegate) {//使用委托 getIntDelegate(a); } public void getInt1(int a) { //方法1 Console.WriteLine("getInt1方法調(diào)用,參數(shù)為:" + a); } public void getInt2(int a) { //方法2 Console.WriteLine("getInt2方法調(diào)用,參數(shù)為:" + a); } } class Program { static void Main(string[] args) { getIntClass gc=new getIntClass(); getIntClass.SetDelegateString(5, gc.getInt1); //方法1,2作為委托的參數(shù) getIntClass.SetDelegateString(10, gc.getInt2); Console.WriteLine("====================="); GetIntDelegate getIntDelegate; getIntDelegate = gc.getInt1; //將方法1,2綁定到委托 getIntDelegate += gc.getInt2; getIntClass.SetDelegateString(100, getIntDelegate); Console.Read(); } } }
輸出結(jié)果,注意兩種方式的不同,第一種將方法作為委托的參數(shù),第二種是將方法綁定到委托。
2.泛型委托之Action,最多傳入16個參數(shù),無返回值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace delegateSummary { class Program { static void Main(string[] args) { TestAction<string>(getString, "WhiteTaken"); //傳入方法 TestAction<int>(getInt, 666); TestAction<int, string>(getStringAndInt, 666, "WhiteTaken"); Console.Read(); } public static void TestAction<T>(Action<T> action,T p1) { //Action傳入一個參數(shù)測試 action(p1); } public static void TestAction<T, P>(Action<T, P> action, T p1, P p2) { //Action傳入兩個參數(shù)測試 action(p1,p2); } public static void getString(string a) { //實(shí)現(xiàn)int類型打印 Console.WriteLine("測試Action,傳入string,并且傳入的參數(shù)為:" +a); } public static void getInt(int a) { //實(shí)現(xiàn)String類型打印 Console.WriteLine("測試Action,傳入int,并且傳入的參數(shù)為:" + a); } public static void getStringAndInt(int a, string name) { //實(shí)現(xiàn)int+string類型打印 Console.WriteLine("測試Action,傳入兩參數(shù),并且傳入的參數(shù)為:" + a+":"+name); } } }
測試結(jié)果:
3.泛型委托之Func,最多傳入16個參數(shù),有返回值。(寫法與Action類似,只是多了返回值)
4.泛型委托之predicate(不是很常用),返回值為bool,用在Array和list中搜索元素。(沒有用到過,等用到了再更新)
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持我們!
您可能感興趣的文章
- 01-10C#中查找Dictionary中的重復(fù)值的方法
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫中的方法
- 01-10關(guān)于nancy中的身份驗(yàn)證
- 01-10C#中的事務(wù)用法實(shí)例分析
- 01-10C#實(shí)現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
- 01-10C#中的正則表達(dá)式介紹
- 01-10C#開發(fā)中的垃圾回收機(jī)制簡析
- 01-10C#語言中的修飾符匯總
- 01-10C#中的 == 和equals()區(qū)別淺析
- 01-10C#基于委托實(shí)現(xiàn)多線程之間操作的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)