泛型编程

  一、泛型概念:用起来类似模板,增加代码的复用性。

  二、举例说明:

      

 1     public class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             int obj = 2;
 6             Test<int> test = new Test<int>(obj);
 7             Console.WriteLine("int:" + test.obj);
 8 
 9             string obj2 = "Hello World";
10             Test<string> test1 = new Test<string>(obj2);
11             Console.WriteLine("string:" + test1.obj);
12             Console.ReadLine();
13         }
14     }
15 
16     // 创建一个泛型类
17     public class Test<T>
18     {
19        //创建一个泛型公有成员变量
20        public T obj;
21        //构造一个泛型变量,在创建对象时,来构造这个对象
22        public Test(T obj)
23        {
24           this.obj = obj;
25        }
26     }

显示:

参考:http://www.cnblogs.com/kid-li/archive/2006/11/29/577045.html  

原文地址:https://www.cnblogs.com/luyuwei/p/3656915.html