C#学习笔记之比较、转换(11.2/3)

    1.处理值类型时后台的一些常见操作:boxing(封箱)和unboxing(拆箱)
    封箱是把值类型转换为System.Object类型,或者转换为由值类型实现的接口类型。拆箱反之。
    封箱是隐式的。拆箱需要进行显示转换。
    封箱允许在项的类型是object的集合中使用值类型。其次在一个区内部机制允许在值类型上调用object。
    因此封箱与拆箱机制允许任何值类型,引用类型和object类型之间进行转换。
    2.is 运算符
可以检查对象是否是给定类型,或者是否可以转换为给定类型。
<operand> is <type>   (type 可以是类类型、接口类型或值类型等)
    3. IComparable和IComparer
IComparable: 在要比较的对象的类中实现。 实现方法: CompareTo(object obj)
System.Collections.IComparer在一个单独的类中实现。 实现方法:Compare(object obj1, object obj2)
对于自己的类排序,必须在类定义中实现IComparable,或者创建一个支持IComparer的类来进行比较。


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 otherPerson = obj as Person;

                Console.WriteLine(this.Age + " . other: " + otherPerson.Age + " , " + (this.Age - otherPerson.Age));

                return this.Age - otherPerson.Age;
            }
            else
            {
                throw new ArgumentException("Object to compare to is not a person object");
            }
        }
    }


    public class PersonCompareName : IComparer
    {
        public static IComparer Default = new PersonCompareName();

        public int Compare(object x, object y)
        {
            if (x is Person && y is Person)
            {
                Console.WriteLine(((Person)x).Name + " , other : " + ((Person)y).Name + " , " + 
Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name));
                return Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name);


            }
            else
            {
                throw new ArgumentException("One or both objects to compare are not Person objects");

            }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();

            list.Add(new Person("Jim", 32));
            list.Add(new Person("Bob", 23));
            list.Add(new Person("Bert", 45));
            list.Add(new Person("Ernie", 54));


            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(PersonCompareName.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();




        }


4.as 运算符: 一般适用于引用类型和装箱的转换。
explicit与implicit 转换运算符;





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值