static void Main(string[] args) { TestGeneric(); } //------------------------------------------------------ // 泛型(Generic) public class SayGenericArray<T> { private T[] array; public SayGenericArray(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; } } public static void TestGeneric() { // 声明一个整型数组 SayGenericArray<int> intArr = new SayGenericArray<int>(5); // 设置值 for (int i = 0; i < 5; i++) { intArr.setItem(i, i); } // 获取值 for (int i = 0; i < 5; i++) { Console.Write(intArr.getItem(i) + " "); } Console.WriteLine(); // 声明一个字符串数组 SayGenericArray<string> charArr = new SayGenericArray<string>(5); // 设置值 for (int i = 0; i < 5; i++) { charArr.setItem(i, "a" + i.ToString()); } // 获取值 for (int i = 0; i < 5; i++) { Console.Write(charArr.getItem(i) + " "); } Console.WriteLine(); Console.ReadLine(); }