Actioin委托

摘自《MSDN》

 

Action<T> delegate to pass a method as a parameter without explicitly declaring a custom delegate." data-guid="c9f6754502a5f6614a1fdb878f0321e1">可以使用 Action<T>Action<T> delegate to pass a method as a parameter without explicitly declaring a custom delegate." data-guid="c9f6754502a5f6614a1fdb878f0321e1"> 委托以参数形式传递方法,而不用显式声明自定义的委托。封装的方法必须与此委托定义的方法签名相对应。也就是说,封装的方法必须具有一个通过值传递给它的参数,并且不能返回值。void." data-guid="66adbaab9b73c1f59400597164592d04">(在 C# 中,该方法必须返回 voidSubEnd Sub construct." data-guid="faaef5aa130e7bf1018538f91430abb4">在 Visual Basic 中,必须通过 SubEnd Sub 结构来定义它。它也可以是返回已忽略的值的方法。)通常,这种方法用于执行某个操作。

 

Action<T> delegate, you do not have to explicitly define a delegate that encapsulates a method with a single parameter." data-guid="931aa91f4a53c7cec3238a145d5564f7">在使用 Action<T> 委托时,不必显式定义一个封装Action<T> delegate, you do not have to explicitly define a delegate that encapsulates a method with a single parameter." data-guid="931aa91f4a53c7cec3238a145d5564f7">只有一个参数Action<T> delegate, you do not have to explicitly define a delegate that encapsulates a method with a single parameter." data-guid="931aa91f4a53c7cec3238a145d5564f7">的方法的委托。

例如:

 

       public delegate void AppendDataDelegate(string message);
        public void AppendData(string message)
        {
            if (richTextBox1.InvokeRequired)
            {
                AppendDataDelegate d = new AppendDataDelegate(AppendData);
                richTextBox1.Invoke(d, new object[] { message });
            }
            else
            {
                Application.DoEvents();
                richTextBox1.AppendText(message + "\n");
            }
        }

或者

 

 this.richTextBox1.Invoke(new MethodInvoker(delegate()
{
           //...........
}));

 

可以写成:

 

      public void AppendData(string message)
        {
            this.richTextBox1.BeginInvoke((Action)delegate(string mess) {
                richTextBox1.AppendText(mess);
            }, message);
        }

 

 

 

Action<T> delegate to print the contents of a List<T> object." data-guid="9ead6d972d1b40cf726034fc03ff5cf2">下面的示例演示如何使用 Action<T> 委托来打印 List<T> 对象的内容。演示如何使用匿名方法将内容显示出来。Action<T> variable." data-guid="178f3a320029f88659e88053d07964e5">请注意该示例不显式声明 Action<T> 变量。List<T>.ForEach method, whose single parameter is an Action<T> delegate." data-guid="91e02e1dab92228bfe7309da9b99b40a">相反,它传递方法的引用,该方法采用单个参数而且不将值返回至 List<T>.ForEach 方法,List<T>.ForEach method, whose single parameter is an Action<T> delegate." data-guid="91e02e1dab92228bfe7309da9b99b40a">其单个参数是一个 Action<T>List<T>.ForEach method, whose single parameter is an Action<T> delegate." data-guid="91e02e1dab92228bfe7309da9b99b40a"> 委托。Action<T> delegate is not explicitly instantiated because the signature of the anonymous method matches the signature of the Action<T> delegate that is expected by the List<T>.ForEach method." data-guid="bcebf671d190d6cddfe8a5e985cf230a">同样,在 C# 示例 中,Action<T> 委托不被显式地实例化,因为匿名方法的签名匹配 List<T>.ForEach 方法所期望的 Action<T> 委托的签名。

例如:

List list = new List();

            list.Add("1");
            list.Add("2");

 


            list.ForEach(delegate(string n) {
                MessageBox.Show(n);//弹出每项值
            });

 

 

你可能感兴趣的:(Actioin委托)