摘要: 转载 usingSystem;//我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一个是处理string数据,或者其他自定义的数据类型,但我们没有办法,只能分别写多个方法处理每个数据类型,因为方法的参数类型不同。有没有一种办法,在方法中传入通用的数据类型,这样不就可以合并代码了吗?泛型的出现就是专门解决这个问题的。namespaceGeneric{classProgram{staticvoidMain(string[]args){//一个普通的类,只能传入int类型的参数。printp=newprint(5);//使用了泛型,只需要在<>中定义参数类型 阅读全文
posted @ 2011-04-22 15:28 勇气 阅读(1776) 评论(0) 推荐(0) 编辑
摘要: C#泛型编程 泛型:通过参数化类型来实现在同一份代码上操作多种数据类型。利用“参数化类型”将类型抽象化,从而实现灵活的复用。例子代码:class Program { static void Main(string[] args) { int obj = 2; Test<int> test = new Test<int>(obj); Console.WriteLine("int:" + test.obj); string obj2 = "hello world"; Test<string> test1 = new Tes 阅读全文
posted @ 2011-04-22 15:27 勇气 阅读(4661) 评论(2) 推荐(2) 编辑