Generic List<T> as parameter on method

C#GenericsC# 3.0

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

You need to make the method generic as well:

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

}

Solution 3 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan EscobedoView Question on Stackoverflow
Solution 1 - C#JaredParView Answer on Stackoverflow
Solution 2 - C#Fredrik MörkView Answer on Stackoverflow
Solution 3 - C#user3418564View Answer on Stackoverflow