c# 委托探究

一.委托类别

1)delegate

delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型

例:public delegate int MethodtDelegate(int x, int y);表示有两个参数,并返回int型

上述委托绑定的方法也必须是返回类型为int,参数为int,且为2个

2)Action Action是无返回值的泛型委托。

Action 表示无参,无返回值的委托

Action<int> 表示有传入参数int;无返回值的委托

Action<int,string> 表示有传入参数int,string ;无返回值的委托

......

3)Func

Func是有返回值的泛型委托

Func<int> 表示无参,返回值为int的委托

Func<int,string>表示传入参数为int类型,返回值为string类型

.......

 Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void


二、委托调用

1.同步调用

Invoke

2.异步调用

BeginInvoke,

BeginInvoke 方法参数不定,但是最后两个参数固定,其中倒数第2个参数,到时第1个参数为必须,若没有,传入null,
倒数第2个参数表示回调函数,倒数第1个参数表示传给回调函数的参数内容

三、调用例子代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmWeiTuo : Form
    {
        public frmWeiTuo()
        {
            InitializeComponent();
        }

        public delegate string testEventHander(string arg);
      


        private void btnAction_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text = "";
            //Action委托,Func 委托 作为方法的类型,
            Test(msg, "huanggang");
            Test(msg, 30);
           
            TestFunc(msgFunc,"huanggang");
            TestFunc(msgFunc,30);


            //delegate 委托
            //将方法msgDelegate 与委托绑定,
            testEventHander testEvent = new testEventHander(msgDelegate);
            MessageBox.Show( msgDelegate("huanggang"));

        }
        /// <summary>
        ///   Action是无返回值的泛型委托 例如Action<int,string> 表示有传入参数int,string无返回值的委托
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action"></param>
        /// <param name="p"></param>
        public void Test<T>(Action<T> action, T p)
        {
            //action(p);

           // action.Invoke(p); //同步调用
            action.BeginInvoke(p,null,null); //异步调用
            this.richTextBox1.Text += "Test" + "\r\n";

        }
        /// <summary>
        ///  Func是有返回值的泛型委托
        /// T1为传入参数,T2为函数返回类型 比如Func(string,int) 表示传入参数是string类型,函数返回类型为int
        /// BeginInvoke  方法参数不定,但是最后两个参数固定,其中倒数第2个参数,到时第1个参数为必须,若没有,传入null,
        /// 倒数第2个参数表示回调函数,倒数第1个参数表示传给回调函数的参数内容
        ///这里使用泛型,对参数类型不限制,灵活,个人认为在框架中经常用到泛型方法,增加框架的灵活性
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="func"></param>
        /// <param name="arg"></param>
        public void TestFunc<T1,T2>(Func<T1,T2> func,T1 arg)
        {

          //  this.richTextBox1.Text += func(arg);

            this.richTextBox1.Text += func.Invoke(arg);//同步
            //this.richTextBox1.Text += func.BeginInvoke(arg,null,null);//异步  这里返回的值是IAsyncResult类型,不是我们的期望值

            //IAsyncResult asyncResult = func.BeginInvoke(arg, null, null);
            //func.EndInvoke(asyncResult);
            //this.richTextBox1.Text += asyncResult.;
        }

        public void msg(string name)
        {
            // 当使用   action.BeginInvoke(p,null,null) 调用时,会报错,提示从不是当前的线程访问
     
             // this.richTextBox1.Text += name+"\r\n";

            //action.BeginInvoke(p,null,null) 调用 ,使用线程匿名委托

            richTextBox1.BeginInvoke(new ThreadStart(delegate ()
                            {
                                richTextBox1.Text += name + "\r\n";
                            }));
            
        }
        public void msg(int age)
        {
            richTextBox1.BeginInvoke(new ThreadStart(delegate()
            {
                richTextBox1.Text += age + "\r\n";
            }));
          
        }
        public string msgFunc(string name)
        {
            //this.richTextBox1.Text += name + "\r\n";
            return name + "\r\n";
        }
        public int msgFunc(int age)
        {
            return age;
        }

        public string msgDelegate(string name)
        {
            //this.richTextBox1.Text += name + "\r\n";
            return name + "\r\n";
        }
        public int msgDelegate(int age)
        {
            return age;
        }
    }
}
View Code

本文参考链接:

https://www.cnblogs.com/yaopengfei/p/8094512.html

https://www.cnblogs.com/mq0036/p/9166893.html

猜你喜欢

转载自www.cnblogs.com/gudaozi/p/12189561.html