为通用类实现IComparable <T>接口以比较类型T

罗哈姆·帕达克蒂姆(Roham Pardakhtim)

我尝试IComparable<T>在泛型类中使用T类型的元素进行比较,但出现以下错误:

“运算符'<'不能应用于类型'T'和'T'的操作数”

我想知道是否可以解决这个问题。这是一个简单的示例,IComparable<T>当我将我的类定义为采用int时有效:

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> 当我将上面的类更改为通用时,现在无法在这里工作:

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;
    }
巴卡基耶夫(M Bakardzhiev)

出现此错误的原因是,除非对Iqualarable进行覆盖,否则不能将其与IComparable配合使用(“ <”,““>”)。

您可以改用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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将通用类型T的不同子类型与Comparable接口进行比较

为什么我需要实现IComparable <T>来比较通用方法中的两个值?

我想实现将参数作为类对象并将参数定义为Class <T>或T类型的通用java方法

Java反射-在通用接口中获取T的实际类型<T>

在特定委托类型的接口中实现 T 参数约束的方法

接受Collection <T>和<T>两种类型的通用类型接口

将通用T的类型解析为C#中的特定接口

从通用类(T)中获取类的类型

实现 IEnumerable<T> 接口

实现接口 Comparator<T>

为MemoryCache(或任何缓存)实现通用Get <T>

为什么Nullable <T>不实现IComparable?

Swift-使用通用方法为不同的<T>类型实现协议

在TypeScript中实现新的通用接口时出现错误“类型IFoo <T>需要构造签名,但类型Foo <T>缺少一个签名”

实现包含T的仅获取属性的接口?针对具有属性T的混凝土类型

通用类::类型参数<T>属性继承

从通用类型T获取“类”对象

如何获得通用类型T的类实例?

实体框架T4:POCO是否实现通用接口?

通用类约束,其中<T>是约束通用类的类型

查找实现具有特定T类型的特定泛型接口的所有类型

通用<T>的响应类

IEquatable <T>的Equals()是否应该通过IComparable <T>的CompareTo()实现?

必须手动实现 IEquatable<T> 而不是 IComparable<T>?

类型为<T>的类,其类型为<T>的可为空

T型通用接口,其中

类型参数“ T”必须为类类型

查询实现通用接口的类的通用类型定义

必需的类型:T提供的:通用Java类中的List <T>