supegong  

委托:回调函数实际上是方法调用的指针,也称为函数指针(即委托)

属性集 修饰符 delegate 函数返回类型 定义的代表标识符(函数形参列表);

修饰符包括new、public、protected、internal和private。

<1>定义这种类型的委托代表了哪种类型的方法delegate int MyDelegate();

<2>然后创建该委托的一个或多个实例。MyDelegate d = new MyDelegate(p.InstanceMethod);

委托使用示例

如下声明一个返回类型为int,无参数的函数的代表MyDelegate:

delegate int MyDelegate();//声明一个代表,注意声明的位置

    public class MyClass

    {

        public int InstanceMethod()//非静态的方法,注意方法为int类型,无参数

        {

            Console.WriteLine("调用了非静态的方法。");

            return 0;

        }       

    }

    public class Class10

    {

        static public void Main()

        {

            MyClass p = new MyClass();

            //用new建立代表类MyDelegate对象,d中存储非静态的方法InstanceMethod的地址

            //MyDelegate d = new MyDelegate(p.InstanceMethod);//或者如下行

            MyDelegate d = p.InstanceMethod;

 

            //d();//调用非静态方法,或者如下行

            d.Invoke();          

        }

    }

委托作为另一个函数的参数:

delegate double DoubleOp(double x);

    class MathsOperations

    {

        public static double MultiplyByTwo(double value)

        {

            return value * 2;

        }

        public static double Square(double value)

        {

            return value * value;

        }

    }

 

    class Class11

    {

        static void Main()

        {

            DoubleOp[] operations ={MathsOperations.MultiplyByTwo,MathsOperations.Square};

            for (int i = 0; i < operations.Length; i++)

            {

                Console.WriteLine("Using operations[{0}]:", i);

                ProcessAndDisplayNumber(operations[i], 3.0);               

                Console.WriteLine();

            }

        }

        //代表作为参数传入(函数名、函数参数)

        static void ProcessAndDisplayNumber(DoubleOp action, double value)

        {

            double result = action(value);

            Console.WriteLine(

            "Value is {0}, result of operation is {1}", value, result);

        }

    }

任何对象的冒泡排序:

//使用方法comparison将sortArray中的数据排序

    class BubbleSorter

    {       

        static public void Sort(object[] sortArray, Comparison comparison)

        {

            for (int i = 0; i < sortArray.Length; i++)

            {

                for (int j = i + 1; j < sortArray.Length; j++)

                {

                    if (comparison(sortArray[j], sortArray[i]))//后一个小于前一个就换

                    {

                        object temp = sortArray[i];

                        sortArray[i] = sortArray[j];

                        sortArray[j] = temp;

                    }

                }

            }

        }

    }

 

    class Employee1

    {

        private string name;

        private decimal salary;

        public Employee1(string name, decimal salary)

        {

            this.name = name;

            this.salary = salary;

        }

        public override string ToString()

        {

            return string.Format("{0}, {1:C}", name, salary);

        }

        public static bool CompareSalary(object x, object y)

        {

            Employee1 e1 = (Employee1)x;

            Employee1 e2 = (Employee1)y;

            return (e1.salary < e2.salary);

        }

    }

    class Class9

    {

        static void Main()

        {

            Employee1[] employees ={new Employee1("Bugs Bunny", 2),

                                    new Employee1("Elmer Fudd ", 1),

                                    new Employee1("Daffy Duck", 3),

                                    };

            BubbleSorter.Sort(employees, Employee1.CompareSalary);

            foreach (var employee in employees)

            {

                Console.WriteLine(employee);

            }

        }

    }

匿名方式、新表达式

delegate string DelegateTest(string val);

        static void Main()

        {

            string mid = ", middle part,";

            //DelegateTest anonDel = delegate(string param)//或者如下行

              DelegateTest anonDel = param =>

                                      {                                     

                                          param += mid;

                                          param += " and this was added to the string.";

                                          return param;

                                      };

            Console.WriteLine(anonDel("Start of string"));}

posted on 2011-02-25 11:02  supegong  阅读(158)  评论(0)    收藏  举报