Various commission Usage (a) .net in

With escalating this net of commission, there have been three.
(Delegate, Action, Func)
application scenarios such as cross-thread update winform UI, thread calls processing, and so on.
1, Delegate delegate keyword, but the actual method prototype declarations that parameter and return types

static void Main(string[] args)
        {
            reslt = 0;

            DeleMeath deleMeathADD = new DeleMeath(add);  //实例化委托,传入符合委托原型的方法名

            int resOne = deleMeathADD(5, 6);

            Console.WriteLine("第一种方式的委托调用,结果是{0}", resOne);

            int resTwo = deleMeathADD.Invoke(1, 2);

            Console.WriteLine("第二种方式的委托调用,结果是{0}", resTwo);

            AsyncCallback asyncCallback = new AsyncCallback(AsyncCallbackMath);

            IAsyncResult result = deleMeathADD.BeginInvoke(1, 5, asyncCallback, null); //开始委托的异步,异步主要是不会阻塞线程

            reslt = deleMeathADD.EndInvoke(result); //结束异步


            //下面是多波委托,有点和事件附加类似
            DeleMeath m, n, c, d;
            m = add;  //加法
            n = Remo;  //减法
                       //c = m + n;  //减法,委托n
                       //c = n + m;  //加法,委托m
                       // c = m - n;  //加法,委托n
            c = n - m;  //加法,委托m

            Console.WriteLine("多播的m值为{0}", m(1, 2));
            Console.WriteLine("多播的n值为{0}", n.Invoke(6, 2));
            Console.WriteLine("多播的c值为{0}", c.Invoke(1, 2));

            Console.ReadKey();
        }


        static int reslt = 0;

        static void AsyncCallbackMath(IAsyncResult result)
        {

            if (result.IsCompleted)  //回调
            {
                Console.WriteLine("委托异步调用完成,计算结果是{0}", reslt);

            }
        }

        delegate int DeleMeath(int parmOne, int parmTwo);  //声明一个委托

        static int add(int a, int b)  //符合委托 原型的方法
        {
            return a + b;
        }

        static int Remo(int a, int b)  //符合委托 原型的方法
        {
            return a - b;
        }

The result is such a run
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_42780928/article/details/92084973