跳轉到內容

.NET 開發基金會/使用泛型集合

來自華夏公益教科書,開放的書籍,開放的世界


系統型別和集合:使用泛型集合


使用泛型集合

[編輯 | 編輯原始碼]

考試目標:透過使用泛型集合,提高 .NET Framework 應用程式的型別安全性和應用程式效能。

(參考 System.Collections.Generic 名稱空間 MSDN )

Collection.Generic 介面

[編輯 | 編輯原始碼]
泛型 IComparable 介面 - MSDN
請注意,IComparable<T> 是 System 名稱空間的成員。
當您建立一個類並希望它與支援排序的泛型型別(例如 SortedList<T> 或 List<T>.Sort())一起使用時,您將使用此介面,而無需指定比較器物件。IComparable<T> 的唯一方法是 CompareTo<T>(T other)。MSDN 上有一個示例。
以下示例為自定義的 Point 類實現 IComparable<T>。該示例使用 List<T> 而不是 SortedList<T> 或 OrderedDictionnary<T>,因為比較是根據點到原點的距離進行的,對於許多點來說,距離值可能相同。
簡單使用 IComparable<T> (C#)

簡單使用 IComparable<T> (C#)

   using System;
   using System.Collections.Generic;
   using System.Text;
   namespace GenericsLab05
   {
       class Program
       {
           static void Main(string[] args)
           {
               List<Point> lst = new List<Point>();
               lst.Add(new Point(-2, -2));
               lst.Add(new Point(1, 1));
               lst.Add(new Point(2, 2));
               // Sort uses IComparable of Point
               lst.Sort();
               foreach (Point pt in lst)
               {
                   Console.WriteLine(pt.ToString());
               }
               // Wait to finish
               Console.WriteLine("Press ENTER to finish");
               Console.ReadLine();
           }
       }
       // This is out custom version of a point
       public struct Point : IComparable<Point>
       {
           public double x;
           public double y;
           public Point(double px, double py)
           {
               x = px;
               y = py;
           }
           // Comparaison done based on distance from origin
           public int CompareTo(Point other)
           {
               return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)).CompareTo
                   (Math.Sqrt(Math.Pow(other.x, 2) + Math.Pow(other.y, 2)));
           }
           public override string ToString()
           {
               return "(" + x.ToString() + "," + y.ToString() + ")";
           }
       }
   }
泛型 ICollection 介面和泛型 IList 介面
泛型 ICollection 介面 - MSDN
泛型 IList 介面 - MSDN
泛型 IComparer 介面和泛型 IEqualityComparer 介面
泛型 IComparer 介面 - MSDN
泛型 IEqualityComparer 介面 - MSDN
泛型 IDictionary 介面 - MSDN
泛型 IEnumerable 介面和泛型 IEnumerator 介面
泛型 IEnumerable 介面 - MSDN
另請參見 ONDotnet
泛型 IEnumerator 介面 - MSDN
IHashCodeProvider 介面 - MSDN - 該介面現已過時(從 .NET 2.0 開始)
泛型字典
[編輯 | 編輯原始碼]
泛型字典類和泛型字典.列舉器結構
泛型字典類 - MSDN
泛型字典.列舉器結構 - MSDN
泛型字典.鍵集合類和字典.鍵集合.列舉器結構
泛型字典.鍵集合類 - MSDN
字典.鍵集合.列舉器結構 - MSDN
泛型字典.值集合類和字典.值集合.列舉器結構
泛型字典.值集合類 - MSDN
字典.值集合.列舉器結構 - MSDN

泛型比較器類和泛型相等比較器類

[編輯 | 編輯原始碼]
泛型比較器類 - MSDN
Comparer<T> 類用作基類,可以輕鬆地實現 IComparer<T> 介面。
該示例與 IComparable<T> 相同,只是現在將派生的 Comparer<T> 物件傳遞給 List<T>.Sort() 方法,而不是在 Point 上實現 IComparable<T> 介面。
這種處理方式有兩個優點:
  • 即使您無法訪問 Point 的原始碼,也可以使用它。
  • 您可以為同一個 Point 類建立多個派生的比較器類。
自定義 Comparer<T> (C#)

自定義 Comparer<T> (C#)

   using System;
   using System.Collections.Generic;
   using System.Text;
   namespace GenericsLab06
   {
       class Program
       {
           static void Main(string[] args)
           {
               List<Point> lst = new List<Point>();
               lst.Add(new Point(-2, -2));
               lst.Add(new Point(1, 1));
               lst.Add(new Point(2, 2));
               // Sort uses IComparable of Point
               lst.Sort(new DistanceComparer());
               foreach (Point pt in lst)
               {
                   Console.WriteLine(pt.ToString());
               }
               // Wait to finish
               Console.WriteLine("Press ENTER to finish");
               Console.ReadLine();
           }
       }
       // This is out custom version of a point
       public struct Point 
       {
           public double x;
           public double y;
           public Point(double px, double py)
           {
               x = px;
               y = py;
           }
           public override string ToString()
           {
               return "(" + x.ToString() + "," + y.ToString() + ")";
           }
       }
       // Derive from base comparer class to implement IComparer<T>
       public class DistanceComparer : Comparer<Point>
       {
           public override int Compare(Point p1, Point p2)
           {
               return Math.Sqrt(Math.Pow(p1.x, 2) + Math.Pow(p1.y, 2)).CompareTo
                   (Math.Sqrt(Math.Pow(p2.x, 2) + Math.Pow(p2.y, 2)));
           }
       }
   }
泛型相等比較器類 - MSDN

泛型 KeyValuePair 結構

[編輯 | 編輯原始碼]
參見 MSDN

泛型列表類、泛型列表.列舉器結構和泛型排序列表類

[編輯 | 編輯原始碼]
泛型列表類 - MSDN
泛型列表類例項只需使用 List<T> 語法宣告,其中 T 是特定型別。
泛型列表.列舉器結構 - MSDN
泛型排序列表類 - MSDN

泛型佇列類和泛型佇列.列舉器結構

[編輯 | 編輯原始碼]
泛型佇列類 - MSDN
泛型佇列.列舉器結構 - MSDN

泛型排序字典類

[編輯 | 編輯原始碼]
參見 MSDN
有關排序列表和排序字典之間差異的解釋,請參見 MSDN

泛型連結串列

[編輯 | 編輯原始碼]
泛型連結串列表示一個雙向連結串列,是一個通用的連結串列。它支援列舉器並實現 ICollection 介面,與 .NET Framework 中的其他類一致。
泛型連結串列類 - MSDN
泛型連結串列.列舉器結構 - MSDN
泛型連結串列節點類 - MSDN

泛型棧類和泛型棧.列舉器結構

[編輯 | 編輯原始碼]
泛型棧類 - MSDN
泛型棧.列舉器結構 - MSDN
華夏公益教科書