• Icomparable和Icomparable<T>的区别


    IComparable<T>
    定义由“值类型或类实现”的通用的比较方法,以为排序实例创建类型特定的比较方法。
    成员: CompareTo 比较当前对象和同一类型的另一对象。

    IComparer<T>
    定义类型为比较两个对象而实现的方法。
    成员: Compare 比较两个对象并返回一个值,指示一个对象是小于、等于还是大于另一个对象。

    从表面看IComparable<T>是排序时使用 IComparer<T>只是比较

    对于这两个接口可能对于初学者来说易混淆。

    下面通过示例来来看下IComparable<T>和ICompare<T>的区别(示例中使用IComparable和ICompare接口)

    对于IComparable接口,它一般是和类绑定在一起的。往往它的实现就存在于将实进行比较的类中,并实现的函数是CompareTo();

    而对于ICompare来说,则不同于上一个接口,它往往独立于要比较的类,在些类中实现的函数是Compare(),并且它的实现也常常用CompareTo()来帮助实现。

    下面来看看一个实例:

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

    namespace Test
    {
        public class Man:IComparable 
        {
            public Man()
            {
                sex="unknow";
                year=0;
            }
            public Man(string sex,int year)
            {
                this.sex=sex;
                this.year=year;
            }
            public int CompareTo(object obj)
            {
                Man other=obj as Man;
                int result=this.sex.CompareTo(other.sex);
                if (result == 0)
                {
                    result = this.year.CompareTo(other.year);
                    return result;
                }
                return result;
            }
            public string sex;
            public int year;
        }
        public class ManComparer : IComparer
        {
            public enum ManComparerType
            {
                sex, year
            }
            private ManComparerType type;
            public ManComparer(ManComparerType type)
            {
                this.type = type;
            }
            public int Compare(object a, object b)
            {
                Man x = a as Man;
                Man y = b as Man;
                switch (type)
                {
                    case ManComparerType.sex:
                        return x.sex.CompareTo(y.sex);
                    case ManComparerType.year:
                        return x.year.CompareTo(y.year);
                    default:
                        throw new ArgumentException("the wrong type:");
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Man[] men ={
                              new Man("mail",4),
                              new Man("mail",5),
                              new Man("femail",67),
                              new Man("femail",45),
                              new Man("mail",58)
                     
                          };
                foreach (Man p in men)
                {
                    Console.WriteLine("he is a " + p.sex + " and is " + p.year);
                }
                Console.WriteLine("\n__________first______________\n");
                Array.Sort(men, new ManComparer(ManComparer.ManComparerType.sex));
                foreach (Man p in men)
                {
                    Console.WriteLine("he is a " + p.sex + " and is " + p.year);
                }
                Console.WriteLine("\n__________second_____________\n");
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Array.Sort(men);
                sw.Stop();
                Console.WriteLine("it takes {0}",sw.Elapsed.ToString());
                foreach (Man p in men)
                {
                    Console.WriteLine("he is a " + p.sex + " and is " + p.year);
                }

            }
        }
    }

    此实例用两个接口对同一个数组进行比较排序。从实现上看可以很清晰地看出它们之间的细微差别。

  • 相关阅读:
    IO复习
    递归
    转换流
    编码与解码
    打印流(printStream)
    Properties
    【转】将Visual Studio武装到底
    【转】VS2008中的自定义格式化代码
    C++开发工具的常用插件
    抽烟的注意事项
  • 原文地址:https://www.cnblogs.com/wpfworld/p/2911045.html
Copyright © 2020-2023  润新知