首页 > 学院 > 开发设计 > 正文

【转载】#324

2019-11-17 03:18:35
字体:
来源:转载
供稿:网友

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

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.

 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 }            

We can use this class as follows:

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());  

原文地址:#324 - A Generic Class Can Have More than One Type Parameter


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表