博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Action<(Of <(T>)> 委托 讲解(MSDN)

Posted on 2009-09-30 11:09  jowo  阅读(1359)  评论(0编辑  收藏  举报

说明:封装一个方法,该方法只采用一个参数并且不返回值。

命名空间:  System
程序集:  mscorlib(在 mscorlib.dll 中)
public delegate void Action<T>(
    T obj
)
类型参数
T

此委托封装的方法的参数类型。

参数

obj
类型:T
此委托封装的方法的参数。

可以使用此委托以参数形式传递方法,而不用显式声明自定义的委托。该方法必须与此委托定义的方法签名相对应。也就是说,封装的方法必须具有一个通过值传递给它的参数,并且不能返回值。(在 C# 中,该方法必须返回 void。在 Visual Basic 中,必须通过 SubEnd Sub 结构来定义它。) 通常,这种方法用于执行某个操作。

018hxwa8.alert_note(zh-cn,VS.90).gif说明:

若要引用具有一个参数并返回值的方法,请改用泛型 Func<(Of <(T, TResult>)>) 委托。

在使用 Action<(Of <(T>)>) 委托时,不必显式定义一个封装只有一个参数的方法的委托。例如,以下代码显式声明了一个名为 DisplayMessage 的委托,并将对 WriteLine 方法或 ShowWindowsMessage 方法的引用分配给其委托实例。

以下示例简化了此代码,它所用的方法是实例化 Action<(Of <(T>)>) 委托,而不是显式定义一个新委托并将命名方法分配给该委托。

您也可以按照以下示例所演示的那样在 C# 中将 Action<(Of <(T>)>) 委托与匿名方法一起使用。(有关匿名方法的简介,请参见匿名方法(C# 编程指南)。)

using System;
using System.Windows.Forms;

public class TestAnonMethod
{
   public static void Main()
   {
      Action<string> messageTarget; 

      if (Environment.GetCommandLineArgs().Length > 1)
         messageTarget = delegate(string s) { ShowWindowsMessage(s); };
      else
         messageTarget = delegate(string s) { Console.WriteLine(s); };

      messageTarget("Hello, World!");
   }

   private static void ShowWindowsMessage(string message)
   {
      MessageBox.Show(message);      
   }
}

您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Action<(Of <(T>)>) 委托实例。(有关 lambda 表达式的简介,请参见 Lambda 表达式(C# 编程指南)。)

using System;
using System.Windows.Forms;

public class TestLambdaExpression
{
   public static void Main()
   {
      Action<string> messageTarget; 

      if (Environment.GetCommandLineArgs().Length > 1)
         messageTarget = s => ShowWindowsMessage(s); 
      else
         messageTarget = s => Console.WriteLine(s);

      messageTarget("Hello, World!");
   }

   private static void ShowWindowsMessage(string message)
   {
      MessageBox.Show(message);      
   }
}
018hxwa8.alert_note(zh-cn,VS.90).gif说明:

Visual Basic 要求 lambda 表达式返回值。因此,在 Visual Basic 中无法将 Action<(Of <(T>)>) 委托与 lambda 表达式一起使用。

ForEachForEach<(Of <(T>)>) 方法都采用 Action<(Of <(T>)>) 委托作为参数。通过使用由委托封装的方法,可以对数组或列表中的每个元素执行操作。此示例使用 ForEach 方法提供说明。

下面的示例演示如何使用 Action<(Of <(T>)>) 委托来打印 List<(Of <(T>)>) 对象的内容。在此示例中,使用 Print 方法将列表的内容显示到控制台上。此外,C# 示例还演示如何使用匿名方法将内容显示到控制台上。

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<String> names = new List<String>();
        names.Add("Bruce");
        names.Add("Alfred");
        names.Add("Tim");
        names.Add("Richard");

        // Display the contents of the list using the Print method.
        names.ForEach(Print);

        // The following demonstrates the anonymous method feature of C#
        // to display the contents of the list to the console.
        names.ForEach(delegate(String name)
        {
            Console.WriteLine(name);
        });
    }

    private static void Print(string s)
    {
        Console.WriteLine(s);
    }
}
/* This code will produce output similar to the following:
 * Bruce
 * Alfred
 * Tim
 * Richard
 * Bruce
 * Alfred
 * Tim
 * Richard