首页 > 代码库 > 泛型的简单用法<T>

泛型的简单用法<T>

 1         private void Form1_Load(object sender, EventArgs e) 2         { 3             Print<string>("here"); 4             Print<int>(1222); 5  6             GenericCollections<string> gcl = new GenericCollections<string>(); 7             gcl.Add("这是一个字符串的示例"); 8             DisplayResult<string>(gcl); 9             GenericCollections<string> gcl2 = new GenericCollections<string>();10             gcl2.Add(true.ToString());11             DisplayResult<string>(gcl2);12             Console.ReadLine();13         }14 15         public void Print<T>(T argument)16         {17             Console.WriteLine(argument.ToString());18         }19 20         void DisplayResult<T>(GenericCollections<T> gcl)21         {22             foreach (T s in gcl)23             {24                 Console.WriteLine(s.ToString());25             }26         }