Generic List<T> as parameter on method

Jonathan Escobedo picture Jonathan Escobedo · Oct 27, 2009 · Viewed 91.7k times · Source

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

Answer

JaredPar picture JaredPar · Oct 27, 2009

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) {
 ...
}