zoukankan      html  css  js  c++  java
  • C#委托冒泡

    委托的实现,就是编译器自行定义了一个类:有三个重要参数1.制定操作对象,2.指定委托方法3.委托链

    看如下一个列子:

     class DelegatePratice
        {
            public static void Sort<T>(IList<T> sortArray, Func<T, T, bool> Comparison)
            {
                bool swapped = true;
                do
                {
                    swapped= false;
                    for (int i = 0; i < sortArray.Count - 1; i++)
                    {
                        if (Comparison(sortArray[i], sortArray[i + 1]))
                        {
                            T temp = sortArray[i];
                            sortArray[i] = sortArray[i + 1];
                            sortArray[i + 1] = temp;
                            swapped = true;
                        }
                    }
                } while (swapped);
               
            }
        }

    定义实体类:
    class Employee
        {
            public Employee(string name, decimal salary)
            {
                this.Name = name;
                this.Salary = salary;
            }
            public string Name
            {
                get;
                set;
            }
            public decimal Salary
            {
                get;
                set;
            }
            /// <summary>
            /// 重写object的ToString方法
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return string.Format("{0},{1:C}",Name,Salary);
            }
            
            /// <summary>
            /// 比较两员工的工资以此排序
            /// </summary>
            /// <param name="e1"></param>
            /// <param name="e2"></param>
            /// <returns></returns>
            public static bool Comparison(Employee e1, Employee e2)
            {
                return e1.Salary > e2.Salary;
            }
        }

     调用

     class Program
        {
            static void Main(string[] args)
            {
                Employee[] employees = { new Employee("simen", 5500), new Employee("shelly", 5900),
                    new Employee("pool", 4300), new Employee("renata", 3800) };
                DelegatePratice.Sort(employees, Employee.Comparison);
                foreach (Employee employee in employees)
                {
                    Console.WriteLine(employee);
                }
                Console.ReadKey();
            }
        }
    }
    

      

      

  • 相关阅读:
    mysql 覆盖索引
    mysql 连接查询 和 子查询
    mysql varchar
    uchome 是如何将数据插入数据库的
    Tomcat5 在windows安装服务
    Linux中错误码及描述查看
    Longines浪琴手表型号解释
    perl 安装 Net::Telnet 模块
    php一些错误的显示问题
    firefox样式表cursor和兼容Ie firefox,解决文字溢出的问题(wordwrap:breakword;wordbreak:breakall)
  • 原文地址:https://www.cnblogs.com/simen-tan/p/5406526.html
Copyright © 2011-2022 走看看