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

编程入门 行业动态 更新时间:2024-10-19 17:34:54
.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 on 2016-11-07 14:27 思如雨 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://wwwblogs/gosky/p/6038767.html

更多推荐

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

本文发布于:2023-06-28 04:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/921468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   Comparison

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!