国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

深入解析C#編程中泛型委托的使用

2020-01-24 01:16:56
字體:
供稿:網(wǎng)友

在看泛型委托之前還需要先了解委托的概念。
這里講的委托有兩種類型一種是有返回值的,另一種是事件委托。

  //定義有返回值的委托   public delegate string GenricDelegate<T, S>(T title, S author);  //定義事件委托。  public delegate void GenricDelegateEnent<E,P>(E Name,P Address);  public class GenericDelegateClass<V,F>  {    //聲明委托    public GenricDelegate<V, F> GdeleValue;    //聲明事件委托    public event GenricDelegateEnent<V, F> GdEvent = null;    public string GetValues(V title, F author)    {      //調(diào)用委托      return GdeleValue(title, author);    }    public GenericDelegateClass()    {    }    public void InvokeEvent(V name, F address)    {      if (GdEvent != null)      {        //調(diào)用委托        GdEvent(name, address);      }    }  }

上面我們定義及調(diào)用了泛型委托,接下來就來梆定委托。

    private void btnDelegate_Click(object sender, EventArgs e)    {      GenericDelegateClass<string, string> gd = new GenericDelegateClass<string, string>();      //將DelegateReturn事件梆定給GdeleValue      gd.GdeleValue = new GenricDelegate<string, string>(DelegateReturn);      //將GenericEvent事件梆定給GdEvent      gd.GdEvent += new GenricDelegateEnent<string, string>(GenericEvent<string,string>);    }    public string DelegateReturn<T,S>(T title,S author)    {      return title.ToString() + author;    }    private void GenericEvent<V, F>(V name, F address)    {      //    }

在這里我們看到我在梆定DelegateReturn的時(shí)候并沒有帶泛型參數(shù)。在這里的泛型參數(shù)其實(shí)是沒什么意義的。因?yàn)樗念愋腿Q于調(diào)用委托的方法的類型。也就是在前面那段代碼中InvokeEvent方法的類型,這里的DelegateReturn要用泛型方法是可以隨時(shí)跟InvokeEvent的參數(shù)類型保持一至。這樣梆定后我們?cè)賮碚{(diào)用gd.GetValues("my generic post","fastyou");這樣調(diào)用的其實(shí)就是DelegateReturn的方法,這就是委托的好處了,同樣調(diào)用gd.InvokeEvent("my generic post","fastyou");就是GenericEvent方法。

委托 可以定義自己的類型參數(shù)。引用泛型委托的代碼可以指定類型參數(shù)以創(chuàng)建已關(guān)閉的構(gòu)造類型,就像實(shí)例化泛型類或調(diào)用泛型方法一樣,如下例所示:

public delegate void Del<T>(T item);public static void Notify(int i) { }Del<int> m1 = new Del<int>(Notify);

C# 2.0 版具有稱為方法組轉(zhuǎn)換的新功能,此功能適用于具體委托類型和泛型委托類型,并使您可以使用如下簡化的語法寫入上一行:

Del<int> m2 = Notify;
在泛型類內(nèi)部定義的委托使用泛型類類型參數(shù)的方式可以與類方法所使用的方式相同。

class Stack<T>{  T[] items;  int index;  public delegate void StackDelegate(T[] items);}

引用委托的代碼必須指定包含類的類型變量,如下所示:

private static void DoWork(float[] items) { }public static void TestStack(){  Stack<float> s = new Stack<float>();  Stack<float>.StackDelegate d = DoWork;}

根據(jù)典型設(shè)計(jì)模式定義事件時(shí),泛型委托尤其有用,因?yàn)榘l(fā)送方參數(shù)可以為強(qiáng)類型,不再需要強(qiáng)制轉(zhuǎn)換成 Object,或反向強(qiáng)制轉(zhuǎn)換。

delegate void StackEventHandler<T, U>(T sender, U eventArgs);class Stack<T>{  public class StackEventArgs : System.EventArgs { }  public event StackEventHandler<Stack<T>, StackEventArgs> stackEvent;  protected virtual void OnStackChanged(StackEventArgs a)  {    stackEvent(this, a);  }}class SampleClass{  public void HandleStackChange<T>(Stack<T> stack, Stack<T>.StackEventArgs args) { }}public static void Test(){  Stack<double> s = new Stack<double>();  SampleClass o = new SampleClass();  s.stackEvent += o.HandleStackChange;}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 垦利县| 大悟县| 蒙山县| 金堂县| 平乐县| 云南省| 榆中县| 深水埗区| 丹巴县| 霸州市| 晋江市| 安庆市| 渭南市| 灵武市| 民丰县| 台北市| 那坡县| 莲花县| 伽师县| 正安县| 巴彦县| 鸡东县| 天镇县| 大英县| 彰武县| 邻水| 平阳县| 桃园县| 郓城县| 丰顺县| 中山市| 海兴县| 若尔盖县| 固原市| 祁阳县| 霍邱县| 岑巩县| 辉南县| 姚安县| 敖汉旗| 广汉市|