views:

85

answers:

1

In the documentation for Compare function in Comparer class it says:

If a implements IComparable, then a. CompareTo (b) is returned; otherwise, if b implements IComparable, then the negated result of b. CompareTo (a) is returned.

But when I test it It seams like it will demand that the first input implements Icomparable. Following code will produce the error:

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;
    }
}

Is it just me or is the docs wrong?

+1  A: 

Refector says that it checks only if a implements IComparable.

public int Compare(object a, object b)
{
if (a == b)
{
    return 0;
}
if (a == null)
{
    return -1;
}
if (b == null)
{
    return 1;
}
if (this.m_compareInfo != null)
{
    string str = a as string;
    string str2 = b as string;
    if ((str != null) && (str2 != null))
    {
        return this.m_compareInfo.Compare(str, str2);
    }
}
IComparable comparable = a as IComparable;
if (comparable == null)
{
    throw new ArgumentException(Environment.GetResourceString("Argument_ImplementIComparable"));
}
return comparable.CompareTo(b);

}

Alex Reitbort
I'm not so sure you can just post decompiled proprietry source here.
Joren