C# 泛型的拳击枚举错误
我不明白这里发生了什么...
我收到以下错误: 类型 'TestApp.TestVal'
不能用作泛型类型或方法 'TestApp.SomeClass
。 没有从 'T'
'TestApp.TestVal'
到 'System.IComparable
的装箱转换。
以下代码会发生此错误
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
接口,看起来不应该有错误......
I don't understand what is going on here...
I've got the following error:
The type 'TestApp.TestVal'
cannot be used as type parameter 'T'
in the generic type or method 'TestApp.SomeClass<T>'
. There is no boxing conversion from 'TestApp.TestVal'
to 'System.IComparable<TestApp.TestVal>'
.
This error happens for the following code:
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>();
}
}
Since the enum is an int
by default and int's implement the IComparable<int>
interface it seems like there shouldn't be an error....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,我不确定将
IComparable
与枚举一起使用是否明智...IEquatable
,当然 - 但比较呢?作为更安全的替代方案; 可以在类中使用
Comparer.Default
,而不是使用通用约束强制使用IComparable
。 这样做的优点是支持IComparable
和IComparable
- 这意味着您的传播限制更少。例如:
这适用于枚举:
Firstly, I'm not sure whether it is sensible to use
IComparable<T>
with an enum...IEquatable<T>
, sure - but comparison?As a safer alternative; rather than mandate the
IComparable<T>
with the generic constraint, perhaps useComparer<T>.Default
inside the class. This has the advantage of supportingIComparable<T>
andIComparable
- and it means you have less constraints to propagate.For example:
This works fine with the enum:
枚举不是从 System.Int32s 派生的 - 它们从 System.Enum 派生,而 System.Enum 不实现
IComparable
(但它确实实现了IComparable
)。尽管默认情况下枚举的基础值是 int,但枚举本身却不是。 因此,两者之间不存在转换。
Enums do not derive from System.Int32s - they derive from System.Enum, which doesn't implement
IComparable<int>
(it does implementIComparable
, though).Although an enum's underlying value is an int by default, the enum itself isn't. Thus, there is no conversion between the two.
Enum 不实现
IComparable
,但它实现了IComparable
。 因此,枚举可以是 where 子句中的 T,例如:但这会产生错误:
然后我想您希望 SomeClass 具有可比性。 为此,它必须自己实现 IComparable。
这是两者的示例(使用公共成员来保持代码简单):
Enum doesn't implement
IComparable<T>
, but it does implementIComparable
. So an enum can be the T in a where clause like:but this gives an error:
And then I suppose you'd like SomeClass to be comparable. To do that, it would have to implement IComparable itself.
Here's an example of both (using a public member to keep the code simple):
在 C# 中,枚举实现了
IComparable
,但不是通用的IComparable
。 我不确定这是为什么,但也许您可以在 where 子句中切换到非通用IComparable
。In C# enums implement
IComparable
, but not the genericIComparable<T>
. I'm not sure why this is, but maybe you could switch to the non-genericIComparable
in your where clause.