创建一个控制台应用程序实现一个委托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyDelegate
{
    delegate int myDelegataHandler(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
    {
        static void Main(string[] args)
        {
            myDelegataHandler mdh = new myDelegataHandler(A.M1);//实现一个委托,简单的理解 方法的委托就是方法的别名,通过委托不但可以执行方法,而且可以将方法传到其他方法中,实现方法回调、
            int sum =mdh(2,2);
            Console.WriteLine(sum.ToString());
            Console.ReadKey();
        }   

    }

    
}

猜你喜欢

转载自blog.csdn.net/laizhuocheng/article/details/89434661