I want to pass in a dataset to a function but it could be different each time (but will always implement IEnumerable. So my call to the function will be:
var items = new List<AssetListItem>();
AimsFunctions.dropdown("Asset", "id", items)
or
var items = new List<AssetCategory>();
AimsFunctions.dropdown("Cats", "id", items)
But how do I work with this in the function?
public static SelectBox dropdown(string name, string key IEnumerable<T> dataset )
{
// Want to work with dataset with Linq here
}
says it doesnt understand what T is in IEnumerable (although neither do i :)
You're trying to use T
as a type argument, so the compiler needs to know which T
you mean. Chances are you want to make your method a generic method too. Fixing the name to follow conventions (and be more readable in general) at the same time, you'd have:
public static SelectBox CreateDropdown<T>(string name, string key, IEnumerable<T> rows)
{
...
}
The <T>
part after the method name is what indicates that it's a generic method. If you're new to generics, you should probably read the MS guide to generics.
See more on this question at Stackoverflow