您编写类或方法的时候,使用数据类型的替代参数,当编译器遇到类的构造函数或方法的函数调用时,它会生成处理特定数据类型的代码。
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) { //declaring an int array MyGenericArray<int> intArray = new MyGenericArray<int>(5); //setting values for (int c = 0; c < 5; c++) { intArray.setItem(c, c*5); } //retrieving the values for (int c = 0; c < 5; c++) { Console.Write(intArray.getItem(c) + " "); } Console.WriteLine(); //declaring a character array MyGenericArray<char> charArray = new MyGenericArray<char>(5); //setting values for (int c = 0; c < 5; c++) { charArray.setItem(c, (char)(c+97)); } //retrieving the values for (int c = 0; c< 5; c++) { Console.Write(charArray.getItem(c) + " "); } Console.WriteLine(); Console.ReadKey(); } } }
编译并执行上述代码时,将生成以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-generics.html
来源:LearnFk无涯教程网
0 5 10 15 20 a b c d e
在前面的示例中,我们使用了泛型类,我们可以使用类型参数声明泛型方法。以下程序说明了概念
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'; //display values before swap: 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); //call swap Swap<int>(ref a, ref b); Swap<char>(ref c, ref d); //display values after swap: 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(); } } }
编译并执行上述代码时,将生成以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-generics.html
来源:LearnFk无涯教程网
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
可以使用类型参数定义泛型委托。如-
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) { //create delegate instances NumberChanger<int> nc1 = new NumberChanger<int>(AddNum); NumberChanger<int> nc2 = new NumberChanger<int>(MultNum); //calling the methods using the delegate objects nc1(25); Console.WriteLine("Value of Num: {0}", getNum()); nc2(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } }
编译并执行上述代码时,将生成以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-generics.html
来源:LearnFk无涯教程网
Value of Num: 35 Value of Num: 175
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)