【发布时间】:2009-06-23 07:25:42
【问题描述】:
我不明白这里发生了什么......
我遇到以下错误:
类型'TestApp.TestVal' 不能用作泛型类型或方法'TestApp.SomeClass<T>' 中的类型参数'T'。没有从'TestApp.TestVal' 到'System.IComparable<TestApp.TestVal>' 的拳击转换。
以下代码会出现此错误:
public enum TestVal
{
First,
Second,
Third
}
public class SomeClass<T>
where T : IComparable<T>
{
public T Stored
{
get
{
return storedval;
}
set
{
storedval = value;
}
}
private T storedval;
}
class Program
{
static void Main(string[] args)
{
//Error is on the next line
SomeClass<TestVal> t = new SomeClass<TestVal>();
}
}
由于枚举默认是int,而int实现了IComparable<int>接口,似乎不应该有错误......
【问题讨论】:
-
int 实现了 IComparable
但这并不意味着 RandomEnumType 实现了 IComparable 。
标签: c# generics enums icomparablet