简体   繁体   English

实现 IComparable<T> 用于比较类型 T 的泛型类的接口

[英]Implementing IComparable<T> Interface for a generic class to compare type T

I tried using IComparable<T> in a generic class to compare elements of type T and I get the following error:我尝试在泛型类中使用IComparable<T>来比较 T 类型的元素,但出现以下错误:

"Operator '<' cannot be applied to operands of type 'T' and 'T'" “运算符 '<' 不能应用于 'T' 和 'T' 类型的操作数”

I was wondering if there could be a work around that.我想知道是否可以解决这个问题。 Here is a simple example that IComparable<T> is working when I define my class to take int:这是一个简单的示例,当我将我的类定义为采用 int 时, IComparable<T>正在工作:

public class IntStack : IComparable<IntStack>
{
    public int[] stack = new int[2];

    public int CompareTo(IntStack other)
    {
        // If the current stack < other stack return -1
        // If the current stack > other stack return +1
        // If current stack entries == other stack entries return 0
        for (var current = 0; current < 2; current++)
        {
            if (stack[current] < other.stack[current])
            {
                return -1;
            }
            else if (stack[current] > other.stack[current])
            {
                return 1;
            }
        }
        return 0;
    }
}

IComparable<T> is now not working here when I change the class above to take generic:当我将上面的类更改为泛型时, IComparable<T>现在在这里不起作用:

public class Mystack<T> : IComparable<Mystack<T>> where T : IComparable
{
    public T[] stack = new T[2];

    public int CompareTo(Mystack<T> other)
    {
        // If the current stack < other stack return -1
        // If the current stack > other stack return +1
        // If current stack entries == other stack entries return 0
        for (var current = 0; current < 2; current++)
        {
            if (stack[current] < other.stack[current])
            {
                return -1;
            }
            else if (stack[current] > other.stack[current])
            {
                return 1;
            }
        }
        return 0;
    }

The reason why you are getting this error is that you cannot use inequality operators ("<", ">") with IComparable, unless you override them.您收到此错误的原因是您不能在 IComparable 中使用不等式运算符(“<”、“>”),除非您覆盖它们。

You can use CompareTo() instead.您可以改用 CompareTo()。

public class Mystack<T> : IComparable<Mystack<T>> where T : IComparable
{
public T[] stack = new T[2];

public int CompareTo(Mystack<T> other)
{
    // If the current stack < other stack return -1
    // If the current stack > other stack return +1
    // If current stack entries == other stack entries return 0
    for (var current = 0; current < 2; current++)
    {
        if (stack[current].CompareTo(other.stack[current]) < 0)
        {
            return -1;
        }
        else if (stack[current].CompareTo(other.stack[current]) > 0)
        {
            return 1;
        }
    }
    return 0;
}

better one can use operator overloading for更好的人可以使用运算符重载

< <

!= These are the operators for comparison when using numeric expression, but if you want to compare object of some sort or some structure then the best option is to overload these 4 operators defined above. != 这些是使用数值表达式时用于比较的运算符,但如果您想比较某种类型或某种结构的对象,那么最好的选择是重载上面定义的这 4 个运算符。

Best way would to instead of using .CompareTo method everytime in a method, use the above 4 after defining overloaded operators最好的方法不是在方法中每次都使用 .CompareTo 方法,而是在定义重载运算符后使用上述 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM