首頁  >  文章  >  後端開發  >  C#程式設計基礎之泛型方法解析(上)

C#程式設計基礎之泛型方法解析(上)

黄舟
黄舟原創
2017-02-06 16:53:341197瀏覽

C#2.0引入了泛型這個特性,由於泛型的引入,在一定程度上極大的增強了C#的生命力,可以完成C#1.0時需要編寫複雜程式碼才可以完成的一些功能。但是作為開發者,對於泛型可謂是又愛又恨,愛的是其強大的功能,以及該特性帶來的效率的提升,恨的是泛型在復雜的時候,會呈現相當複雜的語法結構。


這種複雜不僅是對於初學者,對於一些有開發經驗的.NET開發者,也是一個不那麼容易掌握的特性。


接下來我們來了解C#2.0加入的特性:泛型。


一.泛型的基本特性概述


在實際專案開發中,任何API只要將object作為參數類型和返回類型使用,就可能在某個時候涉及強類型轉換。提到強型別轉換,估計很多開發者第一反應就是「效率」這個次,對於強型別的利弊主要看使用者使用的環境,天底下沒有絕對的壞事,也沒有絕對的好事,有關強型別的問題不是本次的重點,不做重點介紹。


泛型是CLR和C#提供的一種特殊機制,支援另一種形式的程式碼重用,即「演算法重用」。泛型實現了類型和方法的參數化,泛型類型和方法也可以讓參數告訴使用者使用什麼類型。


泛型所帶來的好處:更好的編譯時檢查,更多在程式碼中能直接表現的信息,更多的IDE支持,更好的性能。有人可能會疑問,為什麼泛型會帶來這麼多好處,使用一個無法區分不同類型的常規API,相當於在一個動態環境中存取那個API。

CLR允許建立泛型參考和泛型值類型,但是不允許建立泛型枚舉,且CLR允許建立泛型介面和泛型委託,CLR允許在引用型別、值型別或介面中定義泛型方法。定義泛型類型或方法時,為類型指定了任何變數(如:T)稱為類型參數。 (T是變數名,在原始碼中能夠使用一個資料型別的任何位置,都可以使用T)在C#中泛型參數變數要麼成為T,要麼至少一大寫T開頭。

二.泛型類別、泛型介面和泛型委託概述

1.泛型類別

泛型類型仍然是類型,所以可以從任何類型派生。使用一個泛型類型並指定類型實參時,實際上是在CLR中定義一個新類型對象,新類型對像是從泛型派生自的那個類型派生的。

使用泛型類型參數的一個方法在基尼險那個JIT編譯時,CLR獲取IL,用指定的類型實參進行替換,然後創建恰當的本地代碼。

如果沒有為泛型型別參數提供型別實參,那就麼就是未綁定泛型型別。如果指定了類型實參,則該類型就是已建構類型。

已構造類型可以是開發或封閉的,開發類型還包含一個類ixngcanshu,而封閉類型則不是開發的,類型的每個部分都是明確的。所有程式碼實際上都是在一個封閉的已建構類型的上下文中執行。

泛型類別在.NET的應用主要在集合類別中,大多數集合類別在System.Collections.Generic和System.Collections.ObjectModel類別中。以下簡單的介紹一種泛型集合類別:

(1).SynchronizedCollection:提供一個執行緒安全集合,其中包含泛型參數所指定類型的物件作為元素.

[ComVisible(false)]
public class SynchronizedCollection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
  {
    /// <summary>
    /// 初始化 <see cref="T:System.Collections.Generic.SynchronizedCollection`1"/> 类的新实例。
    /// </summary>
    public SynchronizedCollection();
    /// <summary>
    /// 通过用于对线程安全集合的访问进行同步的对象来初始化 <see cref="T:System.Collections.Generic.SynchronizedCollection`1"/> 类的新实例。
    /// </summary>
    /// <param name="syncRoot">用于对线程安全集合的访问进行同步的对象。</param><exception cref="T:System.ArgumentNullException">
    <paramref name="syncRoot"/> 为 null。</exception>
    public SynchronizedCollection(object syncRoot);
    /// <summary>
    /// 使用指定的可枚举元素列表和用于对线程安全集合的访问进行同步的对象来初始化 <see cref="T:System.Collections.Generic.SynchronizedCollection`1"/> 类的新实例。
    /// </summary>
    /// <param name="syncRoot">用于对线程安全集合的访问进行同步的对象。</param>
    <param name="list">用于初始化线程安全集合的元素的 <see cref="T:System.Collections.Generic.IEnumerable`1"/> 集合。</param>
    <exception cref="T:System.ArgumentNullException"><paramref name="syncRoot"/> 或 <paramref name="list"/> 为 null。</exception>
    public SynchronizedCollection(object syncRoot, IEnumerable<T> list);
    /// <summary>
    /// 使用指定的元素数组和用于对线程安全集合的访问进行同步的对象来初始化 <see cref="T:System.Collections.Generic.SynchronizedCollection`1"/> 类的新实例。
    /// </summary>
    /// <param name="syncRoot">用于对线程安全集合的访问进行同步的对象。</param>
    <param name="list">用于初始化线程安全集合的 <paramref name="T"/> 类型元素的 <see cref="T:System.Array"/>。</param>
    <exception cref="T:System.ArgumentNullException"><paramref name="syncRoot"/> 或 <paramref name="list"/> 为 null。</exception>
    public SynchronizedCollection(object syncRoot, params T[] list);
    /// <summary>
    /// 将项添加到线程安全只读集合中。
    /// </summary>
    /// <param name="item">要添加到集合的元素。</param>
    <exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
    public void Add(T item);
    /// <summary>
    /// 从集合中移除所有项。
    /// </summary>
    public void Clear();
    /// <summary>
    /// 从特定索引处开始,将集合中的元素复制到指定的数组。
    /// </summary>
    /// <param name="array">从集合中复制的 <paramref name="T "/>类型元素的目标 <see cref="T:System.Array"/>。</param>
    <param name="index">复制开始时所在的数组中的从零开始的索引。</param>
    public void CopyTo(T[] array, int index);
    /// <summary>
    /// 确定集合是否包含具有特定值的元素。
    /// </summary>
    /// 
    /// <returns>
    /// 如果在集合中找到元素值,则为 true;否则为 false。
    /// </returns>
    /// <param name="item">要在集合中定位的对象。</param>
    <exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
    public bool Contains(T item);
    /// <summary>
    /// 返回一个循环访问同步集合的枚举数。
    /// </summary>
    /// 
    /// <returns>
    /// 一个 <see cref="T:System.Collections.Generic.IEnumerator`1"/>,用于访问集合中存储的类型的对象。
    /// </returns>
    public IEnumerator<T> GetEnumerator();
    /// <summary>
    /// 返回某个值在集合中的第一个匹配项的索引。
    /// </summary>
    /// 
    /// <returns>
    /// 该值在集合中的第一个匹配项的从零开始的索引。
    /// </returns>
    /// <param name="item">从集合中移除所有项。</param><exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 
    <paramref name="T"/>。</exception>
    public int IndexOf(T item);
    /// <summary>
    /// 将一项插入集合中的指定索引处。
    /// </summary>
    /// <param name="index">要从集合中检索的元素的从零开始的索引。</param><param name="item">要作为元素插入到集合中的对象。</param>
    <exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception>
    <exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
    public void Insert(int index, T item);
    /// <summary>
    /// 从集合中移除指定项的第一个匹配项。
    /// </summary>
    /// 
    /// <returns>
    /// 如果从集合中成功移除了项,则为 true;否则为 false。
    /// </returns>
    /// <param name="item">要从集合中移除的对象。</param>
    public bool Remove(T item);
    /// <summary>
    /// 从集合中移除指定索引处的项。
    /// </summary>
    /// <param name="index">要从集合中检索的元素的从零开始的索引。</param>
    <exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception>
    public void RemoveAt(int index);
    /// <summary>
    /// 从集合中移除所有项。
    /// </summary>
    protected virtual void ClearItems();
    /// <summary>
    /// 将一项插入集合中的指定索引处。
    /// </summary>
    /// <param name="index">集合中从零开始的索引,在此处插入对象。</param><param name="item">要插入到集合中的对象。</param>
    <exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception>
    <exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
    protected virtual void InsertItem(int index, T item);
    /// <summary>
    /// 从集合中移除指定 <paramref name="index"/> 处的项。
    /// </summary>
    /// <param name="index">要从集合中检索的元素的从零开始的索引。</param>
    <exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception>
    protected virtual void RemoveItem(int index);
    /// <summary>
    /// 使用另一项替换指定索引处的项。
    /// </summary>
    /// <param name="index">要替换的对象的从零开始的索引。</param><param name="item">要替换的对象。</param>
    <exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception>
    protected virtual void SetItem(int index, T item);
    /// <summary>
    /// 返回一个循环访问同步集合的枚举数。
    /// </summary>
    /// 
    /// <returns>
    /// 一个 <see cref="T:System.Collections.Generic.IEnumerator`1"/>,用于访问集合中存储的类型的对象。
    /// </returns>
    IEnumerator IEnumerable.GetEnumerator();
    /// <summary>
    /// 从特定索引处开始,将集合中的元素复制到指定的数组。
    /// </summary>
    /// <param name="array">从集合中复制的 <paramref name="T"/> 类型元素的目标 <see cref="T:System.Array"/>。</param>
    <param name="index">复制开始时所在的数组中的从零开始的索引。</param>
    void ICollection.CopyTo(Array array, int index);
    /// <summary>
    /// 向集合中添加一个元素。
    /// </summary>
    /// 
    /// <returns>
    /// 新元素的插入位置。
    /// </returns>
    /// <param name="value">要添加到集合中的对象。</param>
    int IList.Add(object value);
    /// <summary>
    /// 确定集合是否包含具有特定值的元素。
    /// </summary>
    /// <returns>
    /// 如果在集合中找到元素 <paramref name="value"/>,则为 true;否则为 false。
    /// </returns>
  /// <param name="value">要在集合中定位的对象。</param><exception cref="T:System.ArgumentException"><paramref name="value"/> 不是集合所含类型的对象。</exception>
    bool IList.Contains(object value);
    /// <summary>
    /// 确定集合中某个元素的从零开始的索引。
    /// </summary>
    /// 
    /// <returns>
    /// 如果在集合中找到,则为 <paramref name="value"/> 的索引;否则为 -1。
    /// </returns>
    /// <param name="value">集合中要确定其索引的元素。</param>
    int IList.IndexOf(object value);
    /// <summary>
    /// 将某个对象插入到集合中的指定索引处。
    /// </summary>
    /// <param name="index">从零开始的索引,将在该位置插入 <paramref name="value"/>。</param><param name="value">要在集合中插入的对象。</param>
    <exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception>
    <exception cref="T:System.ArgumentException">设置的 <paramref name="value"/> 为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
    void IList.Insert(int index, object value);
    /// <summary>
    /// 从集合中移除作为元素的指定对象的第一个匹配项。
    /// </summary>
    /// <param name="value">要从集合中移除的对象。</param>
    void IList.Remove(object value);
  }

(2).KeyedByTypeCollection:提供一個集合,此集合的項是用作鍵的類型。

[__DynamicallyInvokable]
  public class KeyedByTypeCollection<TItem> : KeyedCollection<Type, TItem>
  {
    /// <summary>
    /// 初始化 <see cref="T:System.Collections.Generic.KeyedByTypeCollection`1"/> 类的新实例。
    /// </summary>
    public KeyedByTypeCollection();
    /// <summary>
    /// 根据指定的对象枚举初始化 <see cref="T:System.Collections.Generic.KeyedByTypeCollection`1"/> 类的新实例。
    /// </summary>
    /// <param name="items">泛型类型 <see cref="T:System.Object"/> 的 <see cref="T:System.Collections.Generic.IEnumerable`1"/>,用于初始化集合。</param>
    <exception cref="T:System.ArgumentNullException"><paramref name="items"/> 为 null。</exception>
    public KeyedByTypeCollection(IEnumerable<TItem> items);
    /// <summary>
    /// 返回集合中第一个具有指定类型的项。
    /// </summary>
    /// 
    /// <returns>
    /// 如果为引用类型,则返回类型 <paramref name="T"/> 的对象;如果为值类型,则返回类型 <paramref name="T"/> 的值。 
    如果集合中不包含类型 <paramref name="T"/> 的对象,则返回类型的默认值:如果是引用类型,默认值为 null;如果是值类型,默认值为 0。
    /// </returns>
    /// <typeparam name="T">要在集合中查找的项的类型。</typeparam>
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public T Find<T>();
    /// <summary>
    /// 从集合中移除具有指定类型的对象。
    /// </summary>
    /// 
    /// <returns>
    /// 从集合中移除的对象。
    /// </returns>
    /// <typeparam name="T">要从集合中移除的项的类型。</typeparam>
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public T Remove<T>();
    /// <summary>
    /// 返回 <see cref="T:System.Collections.Generic.KeyedByTypeCollection`1"/> 中包含的类型 <paramref name="T"/> 的对象的集合。
    /// </summary>
    /// 
    /// <returns>
    /// 一个类型 <paramref name="T"/> 的 <see cref="T:System.Collections.ObjectModel.Collection`1"/>,包含来自原始集合的类型 <paramref name="T"/> 的对象。
    /// </returns>
    /// <typeparam name="T">要在集合中查找的项的类型。</typeparam>
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public Collection<T> FindAll<T>();
    /// <summary>
    /// 从集合中移除所有具有指定类型的元素。
    /// </summary>
    /// 
    /// <returns>
    /// <see cref="T:System.Collections.ObjectModel.Collection`1"/>,包含来自原始集合的类型 <paramref name="T"/> 的对象。
    /// </returns>
    /// <typeparam name="T">要从集合中移除的项的类型。</typeparam>
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public Collection<T> RemoveAll<T>();
    /// <summary>
    /// 获取集合中包含的某个项的类型。
    /// </summary>
    /// 
    /// <returns>
    /// 集合中指定的 <paramref name="item"/> 的类型。
    /// </returns>
    /// <param name="item">集合中要检索其类型的项。</param><exception cref="T:System.ArgumentNullException"><paramref name="item"/> 为 null。</exception>
    [__DynamicallyInvokable]
    protected override Type GetKeyForItem(TItem item);
    /// <summary>
    /// 在集合中的特定位置插入一个元素。
    /// </summary>
    /// <param name="index">从零开始的索引,应在该位置插入 <paramref name="item"/>。</param><param name="item">要在集合中插入的对象。</param>
    <exception cref="T:System.ArgumentNullException"><paramref name="item"/> 为 null。</exception>
    [__DynamicallyInvokable]
    protected override void InsertItem(int index, TItem item);
    /// <summary>
    /// 使用一个新对象替换指定索引处的项。
    /// </summary>
    /// <param name="index">要替换的 <paramref name="item"/> 的从零开始的索引。</param><param name="item">要添加到集合中的对象。</param>
    <exception cref="T:System.ArgumentNullException"><paramref name="item"/> 为 null。</exception>
    [__DynamicallyInvokable]
    protected override void SetItem(int index, TItem item);
  }

CLR支援泛型委託,目的是確保任何類型的物件都能以一種類型安全的方式傳給一個回呼方法。泛型委託允許一個孩子類型實例在傳給一個回呼方法時不執行任何裝箱處理。委託時機只提供了4個方法:一個建構器,一個Invlke方法,一個BeginInvoke方法和一個EndInvoke方法。如果定義的委託類型指定了型別參數,編譯器會定義委託類別的方法,用指定的型別參數取代方法的參數型別和值型別。

以上就是C#程式設計基礎之泛型方法解析(上)的內容,更多相關內容請關注PHP中文網(www.php.cn)!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn