在 Comparer 类中比较函数的文档中,它说:
如果 a 实现了 IComparable,则 a。返回 CompareTo (b);否则,如果 b 实现 IComparable,则 b 的否定结果。返回 CompareTo (a)。
但是当我测试它时,它似乎会要求第一个输入实现 Icomparable。以下代码将产生错误:
class Program
{
static void Main(string[] args)
{
Test t1 = new Test();
Test2 t2 = new Test2();
int i = Comparer.Default.Compare(t1,t2);
}
}
class Test
{
}
class Test2 : IComparable
{
public int CompareTo(object obj)
{
return 0;
}
}
只是我还是文档错了?