79

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

svick
  • 236,525
  • 50
  • 385
  • 514
Jonathan Escobedo
  • 3,977
  • 12
  • 69
  • 90

3 Answers3

184

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) {
 ...
}
svick
  • 236,525
  • 50
  • 385
  • 514
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
23

You need to make the method generic as well:

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

}
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
7
public static  List<T> pesquisa_lista<T>(string campo, string valor, List<T> lista)  
{
   return new List<T>();
}
Brian
  • 5,069
  • 7
  • 37
  • 47
user3418564
  • 71
  • 1
  • 1