泛型允許推遲類或方法中編程元素的數(shù)據(jù)類型規(guī)范的編寫,直到實際在程序中使用它的時候再編寫。換句話說,泛型允許編寫一個可以與任何數(shù)據(jù)類型協(xié)作的類或方法。
你可以通過數(shù)據(jù)類型的替代參數(shù)來編寫類或方法的規(guī)范。當(dāng)編譯器遇到類的構(gòu)造函數(shù)或方法的函數(shù)調(diào)用時,它會生成代碼來處理指定的數(shù)據(jù)類型。下面這個簡單的示例將有助于理解這個概念:
using System;
using System.Collections.Generic;
namespace GenericApplication
{
public class MyGenericArray<T>
{
private T[] array;
public MyGenericArray(int size)
{
array = new T[size + 1];
}
public T getItem(int index)
{
return array[index];
}
public void setItem(int index, T value)
{
array[index] = value;
}
}
class Tester
{
static void Main(string[] args)
{
// 聲明一個整型數(shù)組
MyGenericArray<int> intArray = new MyGenericArray<int>(5);
// 設(shè)置值
for (int c = 0; c < 5; c++)
{
intArray.setItem(c, c*5);
}
// 獲取值
for (int c = 0; c < 5; c++)
{
Console.Write(intArray.getItem(c) + " ");
}
Console.WriteLine();
// 聲明一個字符數(shù)組
MyGenericArray<char> charArray = new MyGenericArray<char>(5);
// 設(shè)置值
for (int c = 0; c < 5; c++)
{
charArray.setItem(c, (char)(c+97));
}
// 獲取值
for (int c = 0; c< 5; c++)
{
Console.Write(charArray.getItem(c) + " ");
}
Console.WriteLine();
Console.ReadKey();
}
}
}
編譯執(zhí)行上述代碼,得到如下結(jié)果:
0 5 10 15 20
a b c d e
泛型是一種可以增強程序功能的技術(shù),表現(xiàn)在如下幾個方面:
在之前的例子中,我們使用過一個泛型類,我們還可以通過類型參數(shù)來聲明泛型方法。下述示例很好地展示了這個概念:
using System;
using System.Collections.Generic;
namespace GenericMethodAppl
{
class Program
{
static void Swap<T>(ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
static void Main(string[] args)
{
int a, b;
char c, d;
a = 10;
b = 20;
c = 'I';
d = 'V';
// 顯示交換之前的值
Console.WriteLine("Int values before calling swap:");
Console.WriteLine("a = {0}, b = {1}", a, b);
Console.WriteLine("Char values before calling swap:");
Console.WriteLine("c = {0}, d = {1}", c, d);
// 調(diào)用 swap 進(jìn)行交換
Swap<int>(ref a, ref b);
Swap<char>(ref c, ref d);
// 顯示交換之后的值
Console.WriteLine("Int values after calling swap:");
Console.WriteLine("a = {0}, b = {1}", a, b);
Console.WriteLine("Char values after calling swap:");
Console.WriteLine("c = {0}, d = {1}", c, d);
Console.ReadKey();
}
}
}
編譯執(zhí)行上述代碼,得到如下結(jié)果:
Int values before calling swap:
a = 10, b = 20
Char values before calling swap:
c = I, d = V
Int values after calling swap:
a = 20, b = 10
Char values after calling swap:
c = V, d = I
你可以通過類型參數(shù)來定義一個泛型委托,如:
delegate T NumberChanger<T>(T n);
泛型委托示例:
using System;
using System.Collections.Generic;
delegate T NumberChanger<T>(T n);
namespace GenericDelegateAppl
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int getNum()
{
return num;
}
static void Main(string[] args)
{
// 創(chuàng)建委托實例
NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);
// 使用委托對象調(diào)用方法
nc1(25);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
編譯執(zhí)行以上代碼,得到如下結(jié)果:
Value of Num: 35
Value of Num: 175