泛型和实现 IComparable

发布于 01-01 16:10 字数 597 浏览 3 评论 0 原文

我对泛型非常陌生,我正在尝试编写一个简单的类,它将是泛型的,但也允许对字符串成员变量的一些描述进行排序。

目前我有一个基本类,但是当我尝试实现接口成员 CompareTo() 时,我在顶部收到一个错误,告诉我它尚未实现。这里有什么问题呢?

using System;

namespace GenericsPracticeConsole.Types
{
    class SortableGenericType<T> : IComparable
    {
        private T t;
        private string stringName;

        public T name
        {
            get { return t; }
            set { t = value; }
        }

        public int CompareTo(SortableGenericType<T> ourObject)
        {
            return stringName.CompareTo(ourObject.stringName);
        }
    }
}

I am very new to generics and I am trying to write a simple class which will be generic but also allow sorting of some description on a string member variable.

At the moment I have a basic class but when I try to implement the interface member CompareTo() I get an error at the top telling me it is not implemented. What is the issue here?

using System;

namespace GenericsPracticeConsole.Types
{
    class SortableGenericType<T> : IComparable
    {
        private T t;
        private string stringName;

        public T name
        {
            get { return t; }
            set { t = value; }
        }

        public int CompareTo(SortableGenericType<T> ourObject)
        {
            return stringName.CompareTo(ourObject.stringName);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风吹短裙飘 2025-01-08 16:10:32

有两个接口IComparableIComparableIComparable 是较旧的(出现在泛型之前),它需要将实例与任意对象进行比较。 IComparable 要求将实例与 U 的实例进行比较。如果您想声明将在 stringName 字段上比较 SortableGenericType 的实例,那么您应该这样做:

class SortableGenericType<T> : IComparable<SortableGenericType<T>>
{
   //
}

如果您还想实现 IComparable:

   class SortableGenericType<T> : IComparable, IComparable<SortableGenericType<T>> 
   {
      private string stringName;
      public T name { get; set; }

      public int CompareTo(SortableGenericType<T> ourObject)
      {
         //I forgot to add this statement:
         if(ourObject == null) 
             return -1; 
         return stringName.CompareTo(ourObject.stringName);
      }

      public int CompareTo(object obj)
      {
         if (obj.GetType() != GetType())
            return -1;
         return CompareTo(obj as SortableGenericType<T>);
      }
   }

如果您的类是一个将保存类型 T 并且您需要这些商品可订购(这不是您所要求的,但这是最常见的情况),而不是要求 TIComparable :

   class SomeCollection<T> where T : IComparable<T>
   {
      private List<T> items; 

      private void Sort()
      {
         //
         T item1;
         T item2;
         if(item1.CompareTo(item2) < 0)
         {
            //bla bla
         }
      }
   }

There are two interfaces IComparable and IComparable<U>. IComparable is the older one (that came before generics) which requires instances to be compared with arbitrary objects. IComparable<U> requires instances to be compared with instances of U. If you want to declare that you will compare instances of SortableGenericType on stringName fields this is what you should do:

class SortableGenericType<T> : IComparable<SortableGenericType<T>>
{
   //
}

If you also want to implement IComparable:

   class SortableGenericType<T> : IComparable, IComparable<SortableGenericType<T>> 
   {
      private string stringName;
      public T name { get; set; }

      public int CompareTo(SortableGenericType<T> ourObject)
      {
         //I forgot to add this statement:
         if(ourObject == null) 
             return -1; 
         return stringName.CompareTo(ourObject.stringName);
      }

      public int CompareTo(object obj)
      {
         if (obj.GetType() != GetType())
            return -1;
         return CompareTo(obj as SortableGenericType<T>);
      }
   }

If your class was a collection that is going to hold items of type T and you needed those items to be orderable (this is not what you ask but it is the most common scenario) than you would require T to be IComparable<T> :

   class SomeCollection<T> where T : IComparable<T>
   {
      private List<T> items; 

      private void Sort()
      {
         //
         T item1;
         T item2;
         if(item1.CompareTo(item2) < 0)
         {
            //bla bla
         }
      }
   }
绳情 2025-01-08 16:10:32

IComparable 定义了方法public int CompareTo(object obj)。请注意参数类型 - 它是 object,而不是您自己的类型。这就是为什么您没有实际实现该接口。

您需要做的是实现 IComparable>

IComparable defines the method public int CompareTo(object obj). Note the parameter type - it's object, not your own type. That's why you aren't actually implementing the interface.

What you need to do is implement IComparable<SortableGenericType<T>>

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