If I have two variables of type T, would it be possible to get all of the values in between the two variables, assuming that both T types are the same?
I am working in .net 2.0 and have built a Range class based on this code: Intersection of Date Ranges
If I know that the type is DateTime, it is easy to loop through the dates easily to get to from the Start value to the End value. Like so:
List<DateTime> values = new List<DateTime>();
for (DateTime d = myRange.Start; d <= myRange.End; d = d.AddDays(1))
{
values.Add(d);
}
But, to make this more generic, is there a way to do this any type T?
Am I asking for something impossible?
You can do something like this with an
IComparable<T>.The increment function needs to be provided manually for a couple reasons: First, some IComparables don’t have a natural increment function: String, for instance. Second, even if there is an increment function, there’s no way to find it. Even with your example of DateTimes, there’s no way to be sure you wanted
AddDays(1), notAddSeconds(1).