c# - How can I get all values between two variables of type T? -


if have 2 variables of type t, possible of values in between 2 variables, assuming both t types same?

i working in .net 2.0 , have built range class based on code: intersection of date ranges

if know type datetime, easy loop through dates start value end value. so:

list<datetime> values = new list<datetime>();  (datetime d = myrange.start; d <= myrange.end; d = d.adddays(1)) {     values.add(d); } 

but, make more generic, there way type t?

am asking impossible?

you can icomparable<t>.

public static list<t> getrange(t start, t end, func<t,t> increment) {     list<t> result = new list<t>();     for(t t = start; t.compareto(end) < 0; t = increment(t))     {         result.add(t);     }     return result; }  list<datetime> values = getrange<datetime>(myrange.start, myrange.end, d => d.adddays(1)); 

the increment function needs provided manually couple reasons: first, icomparables don't have natural increment function: string, instance. second, if there increment function, there's no way find it. example of datetimes, there's no way sure wanted adddays(1), not addseconds(1).


Comments