Implementing IComparable<NotSelf>

Posted by Luc Touraille on Stack Overflow See other posts from Stack Overflow or by Luc Touraille
Published on 2010-03-29T16:18:26Z Indexed on 2010/03/29 16:53 UTC
Read the original article Hit count: 531

Filed under:
|

This might be a trivial question, but I didn't find any information about this: is it "harmful" or considered bad practice to make a type T implement IComparable<S> (T and S being two different types)?

Example:

class Foo : IComparable<int>
{
    public int CompareTo(int other)
    {
        if (other < i) return -1;
        if (other > i) return 1;

        return 0;
    }

    private int i;
}

Should this kind of code be avoided, and if yes, why?

© Stack Overflow or respective owner

Related posts about c#

Related posts about icomparable