清單1 這個控制台應用程式包含一個Customer類和一個基于List<T>的強類型集合Customers。
using System;
using System.Collections.Generic;
using System.Text;
namespace Generics{
class Program{
static void Main(string[] args){
List<Customer> customers = new List<Customer>();
customers.Add(new Customer("Motown-Jobs"));
customers.Add(new Customer("Fatman's"));
foreach (Customer c in customers)
Console.WriteLine(c.CustomerName);
Console.ReadLine();
}
}
public class Customer{
private string customerName = "";
public string CustomerName{
get { return customerName; }
set { customerName = value; }
}
public Customer(string customerName){
this.customerName = customerName;
}
}
}
注意,我們有一個強類型集合-List<Customer>-對這個集合類本身來說不需要寫一句代碼。如果我們想要擴充清單customer,我們可以通過從List<Customer>繼承而派生一個新類。
三、 實作一個泛型類
一種合理的實作某種新功能的方法是在原有的事物上進一步建構。我們已經了解強類型集合,并知道一種不錯的用來建構泛型類的技術是使用一個特定類并删除資料類型。也就是說,讓我們定義一個強類型集合CustomerList,并且來看一下它要把什麼東西轉化成一個泛型類。
清單2定義了一個類CustomerList。後面的部分把CustomerList轉化成List<T>。
清單2定義類CustomerList:
using System;
using System.Collections;
using System.Text;
namespace Generics{
public class CustomerList : CollectionBase{
public CustomerList() { }
public Customer this[int index]{
get { return (Customer)List[index]; }
set { List[index] = value; }
}
public int Add(Customer value)
{return List.Add(value);}
}
}
四、 定義類頭
如果我們定義一個泛型類,我們需要把類頭轉化成一個泛型類。所有我們需要做的是命名參數并且把類名改成某種泛型。List<T>隻有一個參數T,并且因為我們在以一種向後相容的方式工作,是以我們知道類名是List。清單3顯示出清單2中類的新類頭。
清單3 一個泛型類頭顯示出參數化的參數T。
using System;
using System.Collections;
using System.Text;
namespace Generics{
public class List<T> : CollectionBase {}
五、 實作泛型字段
如果我們需要把任何字段轉換成泛型字段,我們将隻需簡單地把它們的類型改變成T(或該字段所描述的任何參數)。泛型List不需要任何字段,但是假定存在一個私有的整型字段叫foo-我們将把它泛型化。我們将如下重新定義它:
private T foo;
當參數T被填充到類中時,List T也将因foo被填充。
六、 定義泛型方法
接下來,我們為所需要的參數化類型定義其它一些特性。這包括屬性,方法,和事件。在我們的執行個體中,在Customer出現的每一處,我們都用參數T替換它。完成後的泛型清單類顯示于清單4中。
清單4 一個基于System.Collections.CollectionBase的輕量級的參數化泛型清單類。
using System;
using System.Collections;
using System.Text;
namespace Generics{
public class List<T> : CollectionBase {
public List(){ }
public T this[int index] {
get { return (T)List[index]; }
set { List[index] = value; }
}
public int Add(T value) {
return List.Add(value);
}
}
}
為了測試該定制清單,注釋掉使用System.Collections.Generic命名空間一句并且把清單4中的List<T>使用在清單1的代碼中;它将以同樣的方式工作。
全面地修改.NET的List<T>是不必要的而且它也包含遠比我們的示例多得多的特性;但是清單4顯示出這種機制對于定義定制泛型類是多麼容易。
七、 增加類型限制
最後要讨論的是限制問題。限制被應用于類或其它特性上并且使用下面的文法:
Where T : constraint_type
例如,任何我們想要通過using語句所使用的,如一個SqlDataReader,必須實作Idisposable接口。這是因為如下方式使用的using語句:
using(Form f = new Form()){...}
就象一個try..finally塊一樣工作-總是清除新建立的資源。其工作原理很簡單,隻需要CLR針對在該using語句中建立的對象發出一個到IDisposable.Dispose的調用即可。例如,在上面這一句中,一個新的表單被建立,并且在using語句退出之前即調用Form.Dispose。
要對一個泛型類施加以確定該類實作了接口IDisposable,我們将添加先行詞where T:Idisposable。在清單4中的泛型清單上施加限制後,我們将重新修改清單4如下面的清單5所示。
清單5 增加一個限制到泛型類以確定我們的List<T>中的所有的值T實作接口Idisposable。
using System;
using System.Collections;
using System.Text;
namespace Generics{
public class List<T> : CollectionBase where t : IDisposable{
public List(){ }
public T this[int index]{
get { return (T)List[index]; }
set { List[index] = value; }
}
public int Add(T value){return List.Add(value);}
}
}
先行詞where的值可以是類,接口,結構,實作一個無參的公共構造器或有一個特定的基類的類。詳見有關幫助文檔。
八、 總結
泛型的設計是用來減少你重複實作的代碼的次數-隻需改變資料類型即可。因為抽象資料結構,如隊列,棧和清單皆是典型的資料結構,是以存在針對這些東西的泛型類完全可以了解。你可以從.NET中派生大量的值-通過使用現有的泛型類,如在System.Collections.Generic命名空間中的那些。
可以肯定,在一段相當長的時間裡,泛型将會象模式和重構等革新一樣對開發帶來越來越大的價值,而且新的資料結構能被轉換成可重用的如泛型等的代碼元素。