分享

委托

 若生安饶 2010-04-08
委托是一种引用方法的类型(方法指针)相当于中介作用
 //C#1.0 标准写法
           // CalutorDelegate cal =new CalutorDelegate( new Program().Add);
            //C#2.0 委托 推断
           // CalutorDelegate cal = new Program().Add;
            //C#2.0 匿名方法
            //CalutorDelegate cal = delegate(int x, int y) { return x + y; };
            //C#2.0 匿名方法
            //CalutorDelegate cal = delegate(int x, int y)
            //{
            //    if (x == null || y == null) return 0;
            //    return x + y;
            //};
            //C#3.0 入(拉姆达)表达式
           // CalutorDelegate cal = (x, y) => {return x + y; };
            //省略return和大括号
           // CalutorDelegate cal = (x, y) => x + y;
            CalutorDelegate cal = (x, y) =>x==null||y==null?0:x + y;
 //query.where(s=>s.customerID.startwith("A"));
可以在调用数据集的方法后().where(s=>s.customerID.startwith("A"));来筛选控制数据的显示
public class Customer
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public static bool Compare(object cust1,object cust2)
        {
            if (((Customer)cust1).ID >= ((Customer)cust2).ID)
                return true;
            else
                return false;
        }

class Program
    {
        public delegate int CalutorDelegate(int x, int y);//类型(方法指针)
        public delegate bool CompareDelegate(Object cust1, Object cust2);
        public int Add(int x, int y)
        {
            return x + y;
        }
        public int Minus(int x, int y)
        {
            return x - y;
        }
        static void Main(string[] args)
        {
            // new Program().Add(10, 12);
            //CalutorDelegate cal = new CalutorDelegate(new Program().Add);
            //Console.WriteLine(cal(10, 12));
            //数据
            Customer[] customers ={
                                     new Customer{ ID=12, Name="abc"},
                                     new Customer{ ID=23, Name="efd"},
                                     new Customer{ ID=1, Name="1wdf"},
                                     new Customer{ ID=120, Name="2abc"},
                                     new Customer{ ID=3, Name="e3fd"},
                                     new Customer{ ID=17, Name="wd4f"}
                                 };//对象初始化器

            //处理结果
            new Program().ObjectCompare(customers, Customer.Compare);//Customer.Compare不打()代表以委托的形式传递参数,打()代表方法的调用
     //显示
            foreach (Customer item in customers)
            {
                Console.WriteLine(item.ID + "  " + item.Name);
            }
        }
        public void ObjectCompare(Object[] customerArray, CompareDelegate compareFunc)
        {
            //指定算法
            for (int i = 0; i < customerArray.Length; i++)//冒泡排序
            {
                for (int j = i + 1; j < customerArray.Length; j++)
                {
                    if (compareFunc(customerArray[i], customerArray[j]))
                    {
                        Object temp = customerArray[i];
                        customerArray[i] = customerArray[j];
                        customerArray[j] = temp;
                    }
                }
            }
        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多