【转载】#324

王朝学院·作者佚名  2016-05-16
窄屏简体版  字體: 小  |  中  |  大  |  超大  

【转载】#324 - A Generic Class Can Have More than One Type ParameterA 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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航