比较

is运算符

is运算符是用来检查对象是不是给定类型,或是否转换为给定类型,返回值为bool值

主要语法如下

bool test1 = 10.GetType() == typeof(int)
<operand> is <type>

运算符的重载

运算符重载需使用静态关键字static和operator关键字和运算符本身

能重载的运算符

一元运算符:+,-,!,~,++,--,true,false

二元运算符:+,-,*,/,%,&,|,^,<<,>>

比较运算符:==,!=,<,>,<=,>=

class AddClass1
{
    public int val;
    public static AddClass1 operator +(AddClass1 op1,AddClass1 op2)
    {
        AddClass1 returnVal = new AddClass1();
        returnVal.val = op1.val + op2.val;
        return returnVal;
    }
    public static AddClass1 operator -(AddClass1 op1,AddClass1 op2)
    {
        AddClass1 returnVal = new AddClass1();
        returnVal.val = op1.val - op2.val;
        return returnVal;
    }
}

 IComparable和IComparer接口

IComparable在要比较的对象的类中实现,可以比较该对象和另一个对象

IComparer在一个单独的类中实现,可以比较任以两个对象

IComparable提供一个方法Compare To(),IComparer也提供一个方法Compare()

IComparable:

class Person : IComparable
{
    public string Name;
    public int Age;
    public Person(string name, int age)
    {
        this.Name = name;
        this.Age = age;
    }
    public int CompareTo(object obj)
    {
        if (obj is Person)
        {
            Person otherPerson = obj as Person;
            return this.Age - otherPerson.Age;
        }
        else
            throw new ArgumentException
                ("Object to compare to is not a person object");
    }
}
Person TT = new Person("TT",23);
Person LL = new Person("LL",22);

TT.CompareTo(LL);

IComparer:

IComparer在使用时需先定义静态的Default,并使用comparer.Default获取一个实例

class PersonComparerName : IComparer
{
    public static IComparer Default = new PersonComparerName();
    public int Compare(object x, object y)
    {
        if (x is Person && y is Person)
        {
            return Comparer.Default.Compare
                (((Person)x).Name, ((Person)y).Name);
        }
        else
            throw new ArgumentException
                ("One or both objects to compare are not Person objects");
    }
}
Person TT = new Person("TT", 23);
Person LL = new Person("LL", 22);

PersonComparerName.Default.Compare(TT, LL);

此处返回值为1,是按照从开始的字母顺序来比较的,若TT<LL,则返回值为-1

对集合排序

对集合排序使用的是集合的sort()方法

ArrayList list = new ArrayList();
list.Add(new Person("TT", 29));
list.Add(new Person("LL", 22));
list.Add(new Person("Xb", 36));
list.Add(new Person("Xh", 33));
//排序前集合内顺序
for (int i = 0; i < list.Count; i++)
    Console.WriteLine($"{(list[i] as Person).Name}({(list[i] as Person).Age})");
Console.WriteLine();
Console.WriteLine("People sorted with default comparer (by age):");
//不给Sort()传入参数
list.Sort();

for (int i = 0; i < list.Count; i++)
    Console.WriteLine($"{(list[i] as Person).Name}({(list[i] as Person).Age})");
Console.WriteLine();
Console.WriteLine("People sorted with nondefault comparer (by name):");
//给Sort()传入参数
list.Sort(PersonComparerName.Default);

for (int i = 0; i < list.Count; i++)
    Console.WriteLine($"{(list[i] as Person).Name}({(list[i] as Person).Age})");

当使用默认的Sort()方法时,必须在类中使用IComparable接口并定义CompareTo()方法,排序的默认方法为CompareTo()方法

当给Sort()方法传入Default时,排序的方法为Compare()方法

posted @ 2018-11-13 17:36  邢韬  阅读(169)  评论(0编辑  收藏  举报