I'm not quite sure I'm understanding how I can utilize generics in C# properly. Say I have the following method. I would like to allow it to work on Lists of any type. Currently I have List where Row is a custom struct, I want to reuse this sort method for half a dozen structs that I make. I thought I could just do List<T>
in the return type and parameter type but it doesn't like that.
public static List<Row> SortResults( List<Row> listRow, string sortColumn, bool ascending)
{
switch (ascending)
{
case true:
return (from r in listRow
orderby r.GetType().GetField(sortColumn).GetValue(r)
select r).ToList<Row>();
case false:
return (from r in listRow
orderby r.GetType().GetField(sortColumn).GetValue(r) descending
select r).ToList<Row>();
default:
return listRow;
}
}