泛型類和泛型方法同時具備可重用性、類型安全和效率,這是非泛型類和非泛型方法無法具備的。泛型通常用在集合和在集合上運行的方法中。.NET Framework 2.0 版類庫提供一個新的命名空間 System.Collections.Generic,其中包含幾個新的基於泛型的集合類。建議面向 2.0 版的所有應用程序都使用新的泛型集合類,而不要使用舊的非泛型集合類,如 ArrayList。有關更多信息,請參見 .NET Framework 類庫中的泛型(C# 編程指南)。
當然,也可以創建自定義泛型類型和方法,以提供自己的通用解決方案,設計類型安全的高效模式。下面的代碼示例演示一個用於演示用途的簡單泛型鏈接列表類。(大多數情況下,建議使用 .NET Framework 類庫提供的 List<T> 類,而不要自行創建類。)在通常使用具體類型來指示列表中所存儲項的類型時,可使用類型參數 T。其使用方法如下:
-
在 AddHead 方法中作為方法參數的類型。
-
在 Node 嵌套類中作為公共方法 GetNext 和 Data 屬性的返回類型。
-
在嵌套類中作為私有成員數據的類型。
注意,T 可用於 Node 嵌套類。如果使用具體類型實例化 GenericList<T>(例如,作為 GenericList<int>),則所有的 T 都將被替換為 int。
// type parameter T in angle brackets
public class GenericList<T>
{
// The nested class is also generic on T
private class Node
{
// T used in non-generic constructor
public Node(T t)
{
next = null;
data = t;
}
private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
// T as private member data type
private T data;
// T as return type of property
public T Data
{
get { return data; }
set { data = value; }
}
}
private Node head;
// constructor
public GenericList()
{
head = null;
}
// T as method parameter type:
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
下面的代碼示例演示客戶端代碼如何使用泛型 GenericList<T> 類來創建整數列表。只需更改類型參數,即可方便地修改下面的代碼示例,創建字符串或任何其他自定義類型的列表:
class TestGenericList
{
static void Main()
{
// int is the type argument
GenericList<int> list = new GenericList<int>();
for (int x = 0; x < 10; x++)
{
list.AddHead(x);
}
foreach (int i in list)
{
System.Console.Write(i + " ");
}
System.Console.WriteLine("\nDone");
}
}
在泛型類和泛型方法中產生的一個問題是,在預先未知以下情況時,如何將默認值分配給參數化類型 T:
-
T 是引用類型還是值類型。
-
如果 T 為值類型,則它是數值還是結構。
給定參數化類型 T 的一個變量 t,只有當 T 為引用類型時,語句 t = null 才有效;只有當 T 為數值類型而不是結構時,語句 t = 0 才能正常使用。 解決方案是使用 default 關鍵字,此關鍵字對於引用類型會返回 null,對於數值類型會返回零。 對於結構,此關鍵字將返回初始化為零或 null 的每個結構成員,具體取決於這些結構是值類型還是引用類型。 對於可以為 null 的值類型,默認返回 System.Nullable(Of T),它像任何結構一樣初始化。
{
class Program
{
static void Main(string[] args)
{
// Test with a non-empty list of integers.
GenericList<int> gll = new GenericList<int>();
gll.AddNode(5);
gll.AddNode(4);
gll.AddNode(3);
int intVal = gll.GetLast();
// The following line displays 5.
System.Console.WriteLine(intVal);
// Test with an empty list of integers.
GenericList<int> gll2 = new GenericList<int>();
intVal = gll2.GetLast();
// The following line displays 0.
System.Console.WriteLine(intVal);
// Test with a non-empty list of strings.
GenericList<string> gll3 = new GenericList<string>();
gll3.AddNode("five");
gll3.AddNode("four");
string sVal = gll3.GetLast();
// The following line displays five.
System.Console.WriteLine(sVal);
// Test with an empty list of strings.
GenericList<string> gll4 = new GenericList<string>();
sVal = gll4.GetLast();
// The following line displays a blank line.
System.Console.WriteLine(sVal);
}
}
// T is the type of data stored in a particular instance of GenericList.
public class GenericList<T>
{
private class Node
{
// Each node has a reference to the next node in the list.
public Node Next;
// Each node holds a value of type T.
public T Data;
}
// The list is initially empty.
private Node head = null;
// Add a node at the beginning of the list with t as its data value.
public void AddNode(T t)
{
Node newNode = new Node();
newNode.Next = head;
newNode.Data = t;
head = newNode;
}
// The following method returns the data value stored in the last node in
// the list. If the list is empty, the default value for type T is
// returned.
public T GetLast()
{
// The value of temp is returned as the value of the method.
// The following declaration initializes temp to the appropriate
// default value for type T. The default value is returned if the
// list is empty.
T temp = default(T);
Node current = head;
while (current != null)
{
temp = current.Data;
current = current.Next;
}
return temp;
}
}
}

