委托简单例子

来源:互联网 发布:excel表格数据相乘 编辑:程序博客网 时间:2024/05/17 04:05

       看一个简单的例子:

     //声明一个委托
    delegate int myDelegateHandler(int a, int b);

    public class A
    {
        //静态的处理方法
        public static int M1(int a, int b)
        {
            int c = 0;
            c = a + b;
            return c;
        }
    }
    //入口类
    public class B
    {
        public static void Main()
        {
            //实例一个委托
            myDelegateHandler mdh = new myDelegateHandler(A.M1);
            //调用委托
            int sum = mdh(2, 2);
            Console.WriteLine(sum.ToString());

        }
    }

0 0