C# 泛型的拳击枚举错误

发布于 2024-07-25 00:47:38 字数 910 浏览 2 评论 0原文

我不明白这里发生了什么...

我收到以下错误: 类型 '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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月无声 2024-08-01 00:47:38

首先,我不确定将 IComparable 与枚举一起使用是否明智...IEquatable,当然 - 但比较呢?

作为更安全的替代方案; 可以在类中使用 Comparer.Default,而不是使用通用约束强制使用 IComparable。 这样做的优点是支持 IComparableIComparable - 这意味着您的传播限制更少。

例如:

public class SomeClass<T> { // note no constraint
    public int ExampleCompareTo(T other) {
        return Comparer<T>.Default.Compare(Stored, other);
    }
    ... [snip]
}

这适用于枚举:

SomeClass<TestVal> t = new SomeClass<TestVal>();
t.Stored = TestVal.First;
int i = t.ExampleCompareTo(TestVal.Second); // -1

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 use Comparer<T>.Default inside the class. This has the advantage of supporting IComparable<T> and IComparable - and it means you have less constraints to propagate.

For example:

public class SomeClass<T> { // note no constraint
    public int ExampleCompareTo(T other) {
        return Comparer<T>.Default.Compare(Stored, other);
    }
    ... [snip]
}

This works fine with the enum:

SomeClass<TestVal> t = new SomeClass<TestVal>();
t.Stored = TestVal.First;
int i = t.ExampleCompareTo(TestVal.Second); // -1
╰◇生如夏花灿烂 2024-08-01 00:47:38

枚举不是从 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 implement IComparable, 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.

静谧 2024-08-01 00:47:38

Enum 不实现 IComparable,但它实现了 IComparable。 因此,枚举可以是 where 子句中的 T,例如:

    where T : IComparable

但这会产生错误:

    where T : IComparable<T>

然后我想您希望 SomeClass 具有可比性。 为此,它必须自己实现 IComparable。

这是两者的示例(使用公共成员来保持代码简单):

public class SomeClass<T>
    : IComparable<SomeClass<T>>
    where T : IComparable
{
    public T storedval;

    public int CompareTo(SomeClass<T> other)
    {
        return storedval.CompareTo(other.storedval);
    }
}

Enum doesn't implement IComparable<T>, but it does implement IComparable. So an enum can be the T in a where clause like:

    where T : IComparable

but this gives an error:

    where T : IComparable<T>

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):

public class SomeClass<T>
    : IComparable<SomeClass<T>>
    where T : IComparable
{
    public T storedval;

    public int CompareTo(SomeClass<T> other)
    {
        return storedval.CompareTo(other.storedval);
    }
}
陪你搞怪i 2024-08-01 00:47:38

在 C# 中,枚举实现了 IComparable,但不是通用的 IComparable。 我不确定这是为什么,但也许您可以在 where 子句中切换到非通用 IComparable

In C# enums implement IComparable, but not the generic IComparable<T>. I'm not sure why this is, but maybe you could switch to the non-generic IComparable in your where clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文