泛型在Class上的实现
class Program { //提高数据重用性 int,string,char,... //类型安全 //性能较好 static void Main(string[] args) { MyGenericArray<int> intArray = new MyGenericArray<int>(5); for (int c = 0; c < 5; c++) { intArray.SetItem(c, c * 5); } for (int c = 0; c < 5; c++) { Console.WriteLine(intArray.GetItem(c)); } MyGenericArray<char> CharArray = new MyGenericArray<char>(5); for (int c = 0; c < 5; c++) { intArray.SetItem(c, c + 97); } for (int c = 0; c < 5; c++) { Console.WriteLine((char)intArray.GetItem(c)); } Console.ReadLine(); } } public class MyGenericArray<T> { private T[] array; public MyGenericArray(int size) { array = new T[size]; } public T GetItem(int index) { return array[index]; } public void SetItem(int index, T value) { array[index] = value; } }
进一步探讨
class Program { //提高数据重用性 int,string,char,... //类型安全 //性能较好 static void Main(string[] args) { MyGenericArray<int, char> intArray = new MyGenericArray<int, char>(5); for (int c = 0; c < 5; c++) { intArray.SetItem(c, c * 5); } for (int c = 0; c < 5; c++) { Console.WriteLine(intArray.GetItem(c)); } MyGenericArray<char, string> CharArray = new MyGenericArray<char, string>(5); for (int c = 0; c < 5; c++) { intArray.SetItem(c, c + 97); } for (int c = 0; c < 5; c++) { Console.WriteLine((char)intArray.GetItem(c)); } Console.ReadLine(); } } public class MyGenericArray<T1, T2> where T1 : struct { private T1[] array; public MyGenericArray(int size) { array = new T1[size]; } public T1 GetItem(int index) { return array[index]; } public void SetItem(int index, T1 value) { array[index] = value; } }
泛型的继承
class Program { //提高数据重用性 int,string,char,... //类型安全 //性能较好 static void Main(string[] args) { MyGenericArray<int> intArray = new MyGenericArray<int>(5); for (int c = 0; c < 5; c++) { intArray.SetItem(c, c * 5); } for (int c = 0; c < 5; c++) { Console.WriteLine(intArray.GetItem(c)); } MyGenericArray<char> CharArray = new MyGenericArray<char>(5); for (int c = 0; c < 5; c++) { intArray.SetItem(c, c + 97); } for (int c = 0; c < 5; c++) { Console.WriteLine((char)intArray.GetItem(c)); } Console.ReadLine(); } } public class MyGenericArray<T> where T : struct { private T[] array; public MyGenericArray(int size) { array = new T[size]; } public T GetItem(int index) { return array[index]; } public void SetItem(int index, T value) { array[index] = value; } } public class SubClass : MyGenericArray<int> { public SubClass(int i) : base(i) { } } public class SubGeneric<T> : MyGenericArray<int> where T : struct { public SubGeneric(int i) : base(i) { } }
泛型方法上的实现
方法:
private static void myswap<T>(ref T a, ref T b) { T temp; temp = a; a = b; b = temp; }
调用:
int aa = 666, bb = 777; myswap<int>(ref aa, ref bb); Console.WriteLine("a:{0},b:{1}", aa, bb);
泛型委托上的实现
delegate T NumberChanger<T>(T n); static int num = 10; public static int AddNum(int p) { return num += p; } public static int MultiNum(int p) { return num *= p; } static void Main(string[] args) { NumberChanger<int> nc1 = new NumberChanger<int>(AddNum); NumberChanger<int> nc2 = new NumberChanger<int>(MultiNum); Console.WriteLine("10+5:{0},*10:{1}", AddNum(5), MultiNum(10));