泛型List< T>作为方法的参数 [英] Generic List<T> as parameter on method

查看:790
本文介绍了泛型List< T>作为方法的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用列表< T> 作为方法的参数,我试试这个语法:

 无效导出(名单< T>的数据,则params字符串[]参数){

}

我得到的编译错误:




类型或命名空间名称'T'找不到(是否缺少using指令或程序集引用?)



解决方案

举一个普通的列表< T> VS绑定列表< INT> 您需要的通用方法以及。这是通过添加一个通用的参数的方法很多在将其添加到一个类型的方式完成的。



请尝试以下

 无效出口< T>(列表< T>的数据,则params字符串[]参数){

}


How can I use a List<T> as a parameter on a method, I try this syntax :

void Export(List<T> data, params string[] parameters){

}

I got compilation error:

The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

解决方案

To take a generic List<T> vs a bound List<int> you need to make the method generic as well. This is done by adding a generic parameter to the method much in the way you add it to a type.

Try the following

void Export<T>(List<T> data, params string[] parameters) {
 ...
}

这篇关于泛型List&LT; T&GT;作为方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆