I\'ve seen some samples of using \'T\' to make a method reuseable for generic collections of different classes, but I\'ve never really gotten into it or understood the samples.
In fact you can do it with a combination of generics and functions, something along the lines of this (untested may not even compile).
[NonAction]
public List ToSelectList(IEnumerable en,
Function text,
Function value,
string defaultOption)
{
var items = en.Select(x => new SelectListItem() { Text = text(x) , Value = value(x) }).ToList();
items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
return items;
}
Then you can dispatch to it with the appropriate lambda functions (or call directly).
[NonAction]
public List ToSelectList(IEnumerable departments,
string defaultOption)
{
return ToSelectList(departments, d => d.Code + '-' + d.Description, d => d.Id.ToString(), defaultOption);
}