Boxing error with generic function

一世执手 提交于 2020-01-07 03:40:23

问题


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;
        }
    }
}

回答1:


Your static Create methods needs to have all of the constraints on it that the Range<T> class applies to its generic argument:

static class Range
{
    public static Range<T> Create<T>(T s, T e, Func<T, T> inc)
        where T : IComparable  //this line was added
    {
        return new Range<T>(s, e, inc);
    }
}

That's all.

Since the Range class requires T to be comparable the Create method needs to ensure that its own T is also comparable.

On a side note, you may consider using IComparable<T> as the constraint for both, rather than IComparable, to ensure static typing of the comparison.

Convention would also state that Range implement IEnumerable<T> rather than IEnumerator<T> so that the same range can be iterated by multiple sources simultaneously, and it also allows you to use the type in a foreach loop and with the many other library methods (i.e. all of LINQ) that are built on the IEnumerable<T> interface.



来源:https://stackoverflow.com/questions/18132889/boxing-error-with-generic-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!