首页 > 其他 > 详细

【转载】A Generic Class Can Have More than One Type Parameter

时间:2014-03-11 21:18:26      阅读:418      评论:0      收藏:0      [点我收藏+]

A generic class includes one or more type parameters that will be substituted with actual types when the class is used. If the class has more than one type parameter, all parameters must be substituted when the class is used.

Here‘s an example of a generic class that stores two objects, each having its own type.

bubuko.com,布布扣
 1 public class ThingContainer<TThing1, TThing2>
 2 {
 3     private TThing1 thing1;
 4     private TThing2 thing2;
 5 
 6     public void SetThings(TThing1 first, TThing2 second)
 7     {
 8         thing1 = first;
 9         thing2 = second;
10     }
11 
12     public string DumpThings()
13     {
14         return string.Format("{0},{1}", thing1.ToString(),             thing2.ToString());
15     }            
16 }            
bubuko.com,布布扣

We can use this class as follows:

bubuko.com,布布扣
1 ThingContainer<string, int> cont1 = new ThingContainer<string, int>();
2 cont1.SetThings("Hemingway", 1899);
3 Console.WriteLine(cont1.DumpThings());   
4 
5 ThingContainer<Dog, DateTime> cont2 = new ThingContainer<Dog, DateTime>();
6 cont2.SetThings(new Dog("Kirby", 14), new DateTime(1998, 5, 1));
7 Console.WriteLine(cont2.DumpThings());  
bubuko.com,布布扣

【转载】A Generic Class Can Have More than One Type Parameter,布布扣,bubuko.com

【转载】A Generic Class Can Have More than One Type Parameter

原文:http://www.cnblogs.com/yuthreestone/p/3593561.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!