閱讀英文

共用方式為


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 中,它必須由 ... End Sub 建構定義 Sub 。它也可以是傳回忽略值的方法。) 一般而言,這類方法是用來執行作業。

注意

若要參考具有一個參數並傳回值的方法,請改用泛型 Func<T,TResult> 委派。

當您使用委派時 Action<T> ,不需要明確定義以單一參數封裝方法的委派。 例如,下列程式碼會明確宣告名為 DisplayMessage 的委派,並將方法的 ShowWindowsMessage 參考 WriteLine 指派給其委派實例。

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 運算式.)

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

ForEachForEach 方法各採用 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, 8, 9, 10
.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, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱