.NET (四)委托第四讲:内置委托Comparison

// 摘要: 
    //     表示比较同一类型的两个对象的方法。
    //
    // 参数: 
    //   x:
    //     要比较的第一个对象。
    //
    //   y:
    //     要比较的第二个对象。
    //
    // 类型参数: 
    //   T:
    //     要比较的对象的类型。
    //
    // 返回结果: 
    //     一个有符号整数,指示 x 与 y 的相对值,如下表所示。 值 含义 小于 0 x 小于 y。 0 x 等于 y。 大于 0 x 大于 y。
    public delegate int Comparison<in T>(T x, T y);

Comparison委托用于比较两个对象的大小。

    class Class4
    {
        public delegate int MyCompare(Customer a, Customer b);
        static void Main1(String[] args)
        {
            Customer c1 = new Customer() { Name = "zhangsan", Age = 18 };
            Customer c2 = new Customer() { Name = "zhangsan", Age = 17 };
            Customer c3 = new Customer() { Name = "zhangsan", Age = 20 };
            Customer c4 = new Customer() { Name = "zhangsan", Age = 10 };
            Customer c5 = new Customer() { Name = "zhangsan", Age = 25 };
            List<Customer> list = new List<Customer>();
            list.Add(c1);
            list.Add(c2);
            list.Add(c3);
            list.Add(c4);
            list.Add(c5);

            Comparison<Customer> comparesion = new Comparison<Customer>(Compare);
            list.Sort(comparesion);


            ObjectDumper.Write(list);

        }
        //比较两个对象大小
        public static int Compare(Customer c1, Customer c2)
        {
            if (c1.Age > c2.Age)
            {
                return 1;
            }
            else if(c1.Age==c2.Age)
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
    }

    class Customer
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

List集合中的 Sort方法,接受该委托:

//
        // 摘要: 
        //     使用指定的 System.Comparison<T> 对整个 System.Collections.Generic.List<T> 中的元素进行排序。
        //
        // 参数: 
        //   comparison:
        //     比较元素时要使用的 System.Comparison<T>。
        //
        // 异常: 
        //   System.ArgumentNullException:
        //     comparison 为 null。
        //
        //   System.ArgumentException:
        //     在排序过程中,comparison 的实现会导致错误。 例如,将某个项与其自身进行比较时,comparison 可能不返回 0。
        public void Sort(Comparison<T> comparison);

也可直接将方法传入:

list.Sort(Compare);

或者省略方法名:

list.Sort(delegate(Customer a, Customer b)
            {
                if (a.Age > b.Age)
                {
                    return 1;
                }
                else if (a.Age == b.Age)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            });

Lamada:

//代码演进
            list.Sort((Customer a, Customer b) =>
            {
                if (a.Age > b.Age)
                {
                    return 1;
                }
                else if (a.Age == b.Age)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            });

 

posted @ 2016-11-07 14:27  思如雨  阅读(458)  评论(0编辑  收藏  举报