C# 经典入门11章,比较

1类型比较

所有的类懂从System.Object中继承了GetType()方法,这个方法和typeof()运算符一起使用,可以确定对象的类型。例如:

if(myObj.GetType()==typeof(MyComplexClass))

{

}

ToString()方法也是从System.Object中继承,提供对象类型的字符串表示。也可以比较这些字符串,但这方式不太好。

2.值比较

使用is运算符来比较值.

先介绍下封箱和拆箱(boxing,unboxing)

 

Person类,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapter11
{
    class Person:IComparable
    {
        public string Name;
        public int Age;
        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public int CompareTo(object obj)
        {
            if (obj is Person)
            {
                Person oterPerson = obj as Person;
                return this.Age - oterPerson.Age;
            }
            else
            {
                throw new ArgumentException(
                    "Object to compare to is not a Person object.");
            }
        }
    }
}

PersonComparerName类,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Chapter11
{
   public  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 object");
           }
       }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Chapter11
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add(new Person("Jim",30));
            list.Add(new Person("Bob", 25));
            list.Add(new Person("Bert", 27));
            list.Add(new Person("Ernie", 22));

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

            }
            Console.WriteLine();
            Console.WriteLine("People sorted with default comparer (by age):");
            list.Sort();
            for (int i=0;i<list.Count;i++)
            {
                Console.WriteLine("{0}({1})",(list[i] as Person ).Name,(list[i] as Person).Age);
 
            }
            Console.WriteLine();
            Console.WriteLine("People sorted with nondefault comparer(by name):");
            list.Sort(PersonComparerName.Default);
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine("{0}({1})", (list[i] as Person).Name, (list[i] as Person).Age);
 
            }
            Console.ReadKey();

        }
    }
}

ClassA obj1 =new ClassA();

ClassD obj2=(ClassD)obj1;

 

 

 


 

 

 

 

 

posted @ 2013-07-17 16:50  秋水惜朝  阅读(175)  评论(0编辑  收藏  举报