If I comment out my static class this compiles fine. However i'd like to have the static class working so I can use the first commented out line in main. My error is
error CS0314: The type 'T' cannot be used as type parameter 'T' in the generic type or method 'DateTest.Range'. There is no boxing conversion or type parameter conversion from 'T' to 'System.IComparable'.
My source is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateTest
{
class Program
{
static void Main(string[] args)
{
//Range.Create(new DateTime(2013, 1, 1), new DateTime(2015, 1, 1), s => s.AddYears(1));
new Range<DateTime>(new DateTime(2013, 1, 1), new DateTime(2015, 1, 1), s => s.AddYears(1));
}
}
static class Range { public static Range<T> Create<T>(T s, T e, Func<T,T> inc) { return new Range<T>(s, e, inc); } }
class Range<T> : IEnumerator<T> where T : IComparable
{
T start, pos, end;
Func<T,T> inc;
public Range(T s, T e, Func<T,T> inc) { pos=start= s; end = e; this.inc = inc; }
public T Current
{
get { return pos; }
}
public void Dispose()
{
throw new NotImplementedException();
}
object System.Collections.IEnumerator.Current
{
get { return pos; }
}
public bool MoveNext()
{
pos = inc(pos);
return pos.CompareTo(end) != 0;
}
public void Reset()
{
pos = start;
}
}
}
Your static
Create
methods needs to have all of the constraints on it that theRange<T>
class applies to its generic argument:That's all.
Since the
Range
class requiresT
to be comparable theCreate
method needs to ensure that its ownT
is also comparable.On a side note, you may consider using
IComparable<T>
as the constraint for both, rather thanIComparable
, to ensure static typing of the comparison.Convention would also state that
Range
implementIEnumerable<T>
rather thanIEnumerator<T>
so that the same range can be iterated by multiple sources simultaneously, and it also allows you to use the type in aforeach
loop and with the many other library methods (i.e. all of LINQ) that are built on theIEnumerable<T>
interface.