C#接口一处错误

namespace ConsoleApp11
{
    interface IComparable
    {
        int CompareTo(object ob);
    }

    class Myclass: IComparable
    {
        int theElement;
        public int _TheElement
        {
            set
            {
                theElement = value;
            }
            get
            {
                return theElement;
            }
        }
        public int CompareTo(object ob)
        {
            Myclass m = (Myclass)ob;
            if (theElement > m.theElement)
                return 1;
            else if (theElement < m.theElement)
                return -1;
            return 0;
        }
    }
    class program
    {
        static void print(Myclass[] my)
        {
            foreach (var m in my)
                Console.Write("{0} ", m._TheElement);
            Console.WriteLine("");
        }
        static void Main()
        {
            Myclass[] M = new Myclass[4];
            for (int i = 0; i < M.Length; ++i)
                M[i] = new Myclass() { _TheElement = M.Length-i };

            print(M);
            Array.Sort(M);
            print(M);
            Console.ReadLine();
        }
    }
}

图片描述

已经实现了接口,报错却显示不能对比

阅读 1.4k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进