Action<T> 委托

定义

封装一个方法,该方法只有一个参数并且不返回值。

C#
public delegate void Action<in T>(T obj);
C#
public delegate void Action<T>(T obj);

类型参数

T

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

这是逆变类型参数。 即,可以使用指定的类型,也可以使用派生程度较低的任何类型。 有关协变和逆变的详细信息,请参阅泛型中的协变和逆变

参数

obj
T

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

示例

以下示例演示如何使用 Action<T> 委托来打印对象的内容 List<T> 。 在此示例中, Print 该方法用于向控制台显示列表的内容。 此外,C# 示例还演示了使用匿名方法向控制台显示内容。 请注意,该示例不显式声明变量 Action<T> 。 相反,它会将引用传递给采用单个参数的方法,并且不向 List<T>.ForEach 该方法返回值,该方法的单个参数是 Action<T> 委托。 同样,在 C# 示例中,Action<T>不会显式实例化委托,因为匿名方法的签名与该方法预期的List<T>.ForEach委托的Action<T>签名匹配。

C#
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);
});

void Print(string s)
{
    Console.WriteLine(s);
}

/* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*/

注解

可以使用 Action<T> 委托将方法作为参数传递,而无需显式声明自定义委托。 封装的方法必须与此委托定义的方法签名相对应。 这意味着封装方法必须具有一个按值传递给它的参数,并且不能返回值。 (在 C# 中,该方法必须返回 void。 在Visual Basic中,它必须由 ... 定义Sub``End Sub construct。 它也可以是返回忽略的值的方法。) 通常,此类方法用于执行操作。

备注

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

使用 Action<T> 委托时,无需显式定义封装具有单个参数的方法的委托。 例如,以下代码显式声明命名 DisplayMessage 的委托,并向委托实例分配对 WriteLine 方法或方法的 ShowWindowsMessage 引用。

C#
using System;
using System.Windows.Forms;

delegate void DisplayMessage(string message);

public class TestCustomDelegate
{
   public static void Main()
   {
      DisplayMessage messageTarget;

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

      messageTarget("Hello, World!");
   }

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

以下示例通过实例化 Action<T> 委托而不是显式定义新委托并向其分配命名方法来简化此代码。

C#
using System;
using System.Windows.Forms;

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

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

      messageTarget("Hello, World!");
   }

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

还可以将 Action<T> 委托与 C# 中的匿名方法一起使用,如以下示例所示。 (有关匿名方法简介,请参阅 Anonymous Methods.)

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<T> 委托实例,如以下示例所示。 (有关 lambda 表达式简介,请参阅 Lambda Expressions.)

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);
   }
}

每个 ForEach 委托作为参数的和 ForEach 方法 Action<T> 。 委托封装的方法允许对数组或列表中的每个元素执行操作。 该示例使用 ForEach 该方法提供插图。

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅