亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2016年02月04日 14:36:04   作者:Fastyou  
這篇文章主要介紹了C#編程中泛型委托的使用,引用泛型委托的代碼可以指定類型參數(shù)以創(chuàng)建已關(guān)閉的構(gòu)造類型,需要的朋友可以參考下

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

  //定義有返回值的委托 
  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ù)其實是沒什么意義的。因為他的類型取決于調(diào)用委托的方法的類型。也就是在前面那段代碼中InvokeEvent方法的類型,這里的DelegateReturn要用泛型方法是可以隨時跟InvokeEvent的參數(shù)類型保持一至。這樣梆定后我們再來調(diào)用gd.GetValues("my generic post","fastyou");這樣調(diào)用的其實就是DelegateReturn的方法,這就是委托的好處了,同樣調(diào)用gd.InvokeEvent("my generic post","fastyou");就是GenericEvent方法。

委托 可以定義自己的類型參數(shù)。引用泛型委托的代碼可以指定類型參數(shù)以創(chuàng)建已關(guān)閉的構(gòu)造類型,就像實例化泛型類或調(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è)計模式定義事件時,泛型委托尤其有用,因為發(fā)送方參數(shù)可以為強類型,不再需要強制轉(zhuǎn)換成 Object,或反向強制轉(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;
}

相關(guān)文章

最新評論