包含类型未实现接口 'System.IComparable' 错误

containing type does not implement interface 'System.IComparable' error

namespace Theprogram.cs
{
    class Program
    {
        static void Main(string[] args)
        {
            CreditCustomer[] creditCustomer = new CreditCustomer[5];
            int x, y;
            bool goodNum;
            for (x = 0; x < creditCustomer.Length; ++x)
            {
                creditCustomer[x] = new CreditCustomer();
                Console.Write("Enter customer number ");
                creditCustomer[x].CustomerNumber = Convert.ToInt32(Console.ReadLine());
                goodNum = true;
                for (y = 0; y < x; ++y)
                {
                    if (creditCustomer[x].Equals(creditCustomer[y]))
                        goodNum = false;
                }
                while (!goodNum)
                {
                    Console.Write("Sorry, the customer number " +
                       creditCustomer[x].CustomerNumber + " is a duplicate. " +
                       "\nPlease reenter ");
                    creditCustomer[x].CustomerNumber = Convert.ToInt32(Console.ReadLine());
                    goodNum = true;
                    for (y = 0; y < x; ++y)
                    {
                        if (creditCustomer[x].Equals(creditCustomer[y]))
                            goodNum = false;
                    }
                }
                Console.Write("Enter name ");
                creditCustomer[x].CustomerName = Console.ReadLine();
                Console.Write("Enter age ");
                creditCustomer[x].Rate = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter amount due ");
                creditCustomer[x].AmountDue = Convert.ToDouble(Console.ReadLine());
            }
            Array.Sort(creditCustomer);
            Array.Sort(customer);
        }
    }
    class Customer : IComparable<Customer>
    {
        private int customerNumber;
        public int CustomerNumber
        {
            get
            {
                return customerNumber;
            }
            set
            {
                customerNumber = value;
            }
        }
        private string customerName;
        public string CustomerName
        {
            get
            {
                return customerName;
            }
            set
            {
                customerName = value;
            }
        }
        private double amountDue;
        public double AmountDue
        {
            get
            {
                return amountDue;
            }
            set
            {
                amountDue = value;
            }
        }
        public Customer(int num, string name, int amt)
        {
        CustomerNumber = num;
        CustomerName = name;
        AmountDue = amt;
        }
        public Customer(): this(9, "ZZZ", 0)
        {
        }
        public override bool Equals(Object e)
        {
            bool equal;
            Customer temp = (Customer)e;
            if (this.CustomerNumber == temp.CustomerNumber)
                equal = true;
            else
                equal = false;
            return equal;
        }
        public override int GetHashCode()
        {
            return CustomerNumber;
        }
        public override string ToString()
        {
            return (GetType() + " Credit Customer " + CustomerNumber + " " + CustomerName +
              " Amount Due is " + AmountDue.ToString("C") + " Interest rate is ");
        }
        protected virtual int IComparable.CompareTo(Object o)
        {
            int returnVal;
            Customer temp = (Customer)o;
            if (this.CustomerNumber >
                temp.CustomerNumber)
                returnVal = 1;
            else
                if (this.CustomerNumber < temp.CustomerNumber)
                    returnVal = -1;
                else
                    returnVal = 0;
            return returnVal;
        }
    }
    class CreditCustomer : Customer, IComparable<CreditCustomer>
    {
        public int Rate {get; set;}
        int MonthlyPayment { get; set; }
        public CreditCustomer(int customerNumber, string customerName, int amountDue, int Rate) : base(customerNumber, customerName, amountDue)
        { }
        public CreditCustomer(): this(0, "", 0, 0)
        { }
        public override string ToString()
        {
            return (GetType() + " Credit Customer " + CustomerNumber + " " + CustomerName +
              " Amount Due is " + AmountDue.ToString("C") + " Interest rate is " + Rate + " Monthly Payment is " + MonthlyPayment);
        }
        int IComparable.CompareTo(object o)
        {
            int returnVal;
            CreditCustomer temp = (CreditCustomer)o;
            if (this.CustomerNumber > temp.CustomerNumber)
                returnVal = 1;
            else
                if (this.CustomerNumber < temp.CustomerNumber)
                    returnVal = -1;
                else
                    returnVal = 0;
            return returnVal;
        }
    }

}

我在 C# 中为我的 parent 和 child 类 实现 Icomparable 时遇到问题,因为我收到错误: 没有实现接口成员'System.IComparable.CompareTo

我真的需要有关如何实现 Icomparable 接口的帮助,以便我可以在我的 object 数组上使用 compareTo 方法。任何帮助将不胜感激。

如果您正在使用 Visual Studio 并且不确定如何实现接口,只需右键单击 class 声明中的 IComparable

class CreditCustomer : Customer, IComparable<CreditCustomer>

和select 实施接口以及两个选项中的任何一个。

CreditCustomer 中实现的正确签名是:

public int CompareTo(CreditCustomer other)
{
}

int IComparable<CreditCustomer>.CompareTo(CreditCustomer other)
{
}

请注意,除非您将对象转换为 IComparable<CreditCustomer>,否则第二个签名会隐藏该方法。

正如 SLaks 在他的评论中提到的,实际上有两个名为 IComparable 的不同接口,很容易混淆:

  1. IComparable 是一个较旧的非通用接口,它允许在 object 和任何其他 object 之间进行比较。没有类型检查 - 您的 CreditCustomer 可以与 intList<string> 或任何东西进行比较。这个接口定义了一个方法,CompareTo(object obj)。这是您在 class.

  2. 中使用的方法
  3. IComparable<T> 是更新的通用接口。它与 IComparable 非常相似,但强制执行类型检查 - 它只比较 T 和 T,因此您不必编写大量样板代码来确保没有人尝试比较您的 CreditCustomerDog 之类的。它还定义了一个方法——CompareTo(T obj)。这是您将 class 标记为实现的接口。

尽管命名相似,但就编译器而言,这两个是两个不同的接口。如果您没有接受 CreditCustomer 参数的 CompareTo 方法,那么您 不是 实现 IComparable<CreditCustomer>,这就是它给您的原因一个错误。要么更改您的方法签名以匹配接口,要么将您正在使用的接口更改为与方法签名匹配的接口。应该是前者。