IComparer和IComparable的区别

一:IComparable
//IComparable接口只有一个方法:CompareTo(),这个方法沿用了传统的C函数库里的strcmp函数的实现原则:
//如果当前对象比目标对象小,它的返回值小于0;如果相等就返回0;如果当前对象比目标对象大,返回值就大于0。
//函数原型如下:
public interface IComparable
{
    int CompareTo(object obj);
}

public struct Customer : IComparable
{
    private readonly string _name;
    public Customer( string name )
    {
        _name = name;
    }
    //要检测参数的运行时类型。不正确的代码可以用任何类型做为参数来调用CompareTo方法。
    //还有,正确的参数还必须进行装箱与拆箱后才能提供实际的比较。
    //可以重载CompareTo()方法,让它只对Customer 对象操作:
    public int CompareTo( object right )
    {
        if ( ! ( right is Customer ) )
            throw new ArgumentException( "Argument not a customer", "right" );
        Customer rightCustomer = ( Customer )right;
        return _name.CompareTo( rightCustomer._name );
    }
}
public struct Customer : IComparable
{
    private string _name;
    public Customer( string name )
    {
        _name = name;
    }
    //只能通过强制转化为IComparable 接口后才能访问:
    //Customer c1;
    //Employee e1;
    //if ( ( c1 as IComparable ).CompareTo( e1 ) > 0 )
    //    Console.WriteLine( "Customer one is greater" );
    int IComparable.CompareTo( object right )
    {
        if ( ! ( right is Customer ) )
            throw new ArgumentException( "Argument not a customer", "right");
        Customer rightCustomer = ( Customer )right;
        return CompareTo( rightCustomer );
    }
    //Customer c1;
    //Employee e1;
    //if ( c1.CompareTo( e1 ) > 0 )
    //    Console.WriteLine( "Customer one is greater" );
    public int CompareTo( Customer right )
    {
        return _name.CompareTo( right._name );
    }
    public static bool operator < ( Customer left, Customer right )
    {
        return left.CompareTo( right ) < 0;
    }
    public static bool operator <=( Customer left, Customer right )
    {
        return left.CompareTo( right ) <= 0;
    }
    public static bool operator >( Customer left, Customer right )
    {
        return left.CompareTo( right ) > 0;
    }
    public static bool operator >=( Customer left, Customer right )
    {
        return left.CompareTo( right ) >= 0;
    }
}

二:IComparer
//角色的 IComparer 旨在提供其他比较机制。 例如, 您可能希望提供排序是按几个字段或属性, 类升序和降序对同一字段, 或两者
public interface IComparer
{
    int Compare(object x, object y);
}
//Array.Sort(arrayOfCutstomers,Customer._revComp);
public struct Customer : IComparable
{
    private string _name;
    private double _revenue;

    private static RevenueComparer _revComp = null;
    public static IComparer RevenueCompare
    {
        get
        {
            if ( _revComp == null )
                _revComp = new RevenueComparer();
            return _revComp;
        }
    }

    private class RevenueComparer : IComparer
    {
        int IComparer.Compare( object left, object right )
        {
            if ( ! ( left is Customer ) )
                throw new ArgumentException("Argument is not a Customer", "left");
            if (! ( right is Customer) )
                throw new ArgumentException("Argument is not a Customer", "right");
            Customer leftCustomer = ( Customer ) left;
            Customer rightCustomer = ( Customer ) right;
            return leftCustomer._revenue.CompareTo(rightCustomer._revenue);
        }
    }
}

你可能感兴趣的:(struct,object,String,null,Class,interface)