C# 通用 List<T> 作为方法的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1633690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:34:07  来源:igfitidea点击:

Generic List<T> as parameter on method

c#genericsc#-3.0

提问by Jonathan Escobedo

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

如何使用 aList<T>作为方法的参数,我尝试使用以下语法:

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?)

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

采纳答案by JaredPar

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.

要采用泛型List<T>与界限,List<int>您还需要使方法通用。这是通过向方法添加泛型参数来完成的,这与将其添加到类型的方式非常相似。

Try the following

尝试以下

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

回答by Fredrik M?rk

You need to make the method generic as well:

您还需要使该方法通用:

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

}

回答by user3418564

public static  List<T> pesquisa_lista<T>(string campo, string valor, List<T> lista)  
{
   return new List<T>();
}