与泛型函数的拳击错误 [英] Boxing error with generic function
问题描述
如果我注释掉我的静态类,这个编译好。不过,我想有静态类工作,所以我可以使用主注释掉的第一行。我的错误是
错误CS0314:类型'T'不能用作通用类型或方法'DateTest中的类型参数'T' 。范围'。没有从'T'到'System.IComparable'的装箱转换或类型参数转换。
我的源代码是
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
命名空间DateTest
{
class程序
{
static void Main(string [] args)
{
// Range.Create(new DateTime(2013,1,1),new DateTime(2015,1,1),s => s.AddYears(1)); (新日期时间(2013,1,1),新日期时间(2015,1,1),s => s.AddYears(1));
新范围< DateTime>
}
}
静态类范围{public static Range< T>创建< T>(T s,T e,Func< T,T> inc){返回新的Range T(s,e,inc); }}
class Range< T> :IEnumerator< T> 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();}
}
对象System.Collections.IEnumerator.Current
{
get {return pos; }
}
public bool MoveNext()
{
pos = inc(pos);
返回pos.CompareTo(end)!= 0;
}
public void Reset()
{
pos = start;
您的静态 Create
方法需要具有所有约束,即 Range
类适用于其泛型参数:
静态类范围
{
public静态范围< T>创建< T>(T s,T e,Func< T,T> inc)
其中T:IComparable //这行添加了
{
返回新Range ,e,inc);
}
}
就这样。
由于
范围
类要求 T
与 Create
方法需要确保它自己的 T
也具有可比性。 请注意,您可以考虑使用 IComparable< T>
作为约束条件,而不是 IComparable
,以确保静态类型
约定还会声明 Range
执行 IEnumerable
IEnumerator< T>
,以便同一个范围可以同时被多个源迭代,并且还允许您在 foreach
循环以及构建在 IEnumerable< T>
接口上的许多其他库方法(即所有LINQ) 。
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 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.
这篇关于与泛型函数的拳击错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!