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;
}
}
}
where T : IComparable<T>
– Ashraf