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();
}
}
}
已经实现了接口,报错却显示不能对比
回答:
它需要实现的接口不是你自己写的 IComparable, 而是 System.IComparabale
以上是 C#接口一处错误 的全部内容, 来源链接: utcz.com/p/190311.html