閱讀英文

共用方式為


Action<T1,T2> 代理人

定義

封裝具有 2 個參數且沒有傳回值的方法。

C#
public delegate void Action<in T1,in T2>(T1 arg1, T2 arg2);
C#
public delegate void Action<T1,T2>(T1 arg1, T2 arg2);

類型參數

T1

此委派封裝之方法的第一個參數類型。

這是反變數的型別參數。 也就是說,您可以使用您指定的類型,或衍生程度較低的任何類型。 如需共變數與反變數的詳細資訊,請參閱泛型中的共變數與反變數
T2

此委派封裝之方法的第二個參數類型。

這是反變數的型別參數。 也就是說,您可以使用您指定的類型,或衍生程度較低的任何類型。 如需共變數與反變數的詳細資訊,請參閱泛型中的共變數與反變數

參數

arg1
T1

由這個委派所封裝之方法的第一個參數。

arg2
T2

此委派封裝之方法的第二個參數。

備註

您可以使用 Action<T1,T2> 委派將方法當做參數傳遞,而不需明確宣告自訂委派。 封裝的方法必須對應至這個委派所定義的方法簽章。 這表示封裝的方法必須有兩個參數,兩個參數都是以傳值方式傳遞給它,而且不能傳回值。 (在 C# 中,方法必須傳回 void 。 在 F# 中,方法或函式必須傳回單位。 在Visual Basic中,它必須由 ... 定義 Sub``End Sub construct: 它也可以是傳回忽略之值的方法。) 通常,這類方法會用來執行作業。

注意

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

當您使用委派時 Action<T1,T2> ,不需要明確定義封裝具有兩個參數之方法的委派。 例如,下列程式碼會明確宣告名為 的 ConcatStrings 委派。 然後,它會將兩個方法的其中一個參考指派給其委派實例。 一種方法會將兩個字串寫入主控台;第二個會將兩個字串寫入檔案。

C#
using System;
using System.IO;

delegate void ConcatStrings(string string1, string string2);

public class TestDelegate
{
   public static void Main()
   {
      string message1 = "The first line of a message.";
      string message2 = "The second line of a message.";
      ConcatStrings concat;

      if (Environment.GetCommandLineArgs().Length > 1)
         concat = WriteToFile;
      else
         concat = WriteToConsole;

      concat(message1, message2);
   }

   private static void WriteToConsole(string string1, string string2)
   {
      Console.WriteLine("{0}\n{1}", string1, string2);
   }

   private static void WriteToFile(string string1, string string2)
   {
      StreamWriter writer = null;
      try
      {
         writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
         writer.WriteLine("{0}\n{1}", string1, string2);
      }
      catch
      {
         Console.WriteLine("File write operation failed...");
      }
      finally
      {
         if (writer != null) writer.Close();
      }
   }
}

下列範例會具現化委派, Action<T1,T2> 而不是明確定義新的委派,並將具名方法指派給它,藉此簡化此程式碼。

C#
using System;
using System.IO;

public class TestAction2
{
   public static void Main()
   {
      string message1 = "The first line of a message.";
      string message2 = "The second line of a message.";
      Action<string, string> concat;

      if (Environment.GetCommandLineArgs().Length > 1)
         concat = WriteToFile;
      else
         concat = WriteToConsole;

      concat(message1, message2);
   }

   private static void WriteToConsole(string string1, string string2)
   {
      Console.WriteLine("{0}\n{1}", string1, string2);
   }

   private static void WriteToFile(string string1, string string2)
   {
      StreamWriter writer = null;
      try
      {
         writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
         writer.WriteLine("{0}\n{1}", string1, string2);
      }
      catch
      {
         Console.WriteLine("File write operation failed...");
      }
      finally
      {
         if (writer != null) writer.Close();
      }
   }
}

您也可以在 Action<T1,T2> C# 中搭配匿名方法使用委派,如下列範例所示。 (如需匿名方法的簡介,請參閱 Anonymous Methods.)

C#
using System;
using System.IO;

public class TestAnonymousMethod
{
   public static void Main()
   {
      string message1 = "The first line of a message.";
      string message2 = "The second line of a message.";
      Action<string, string> concat;

      if (Environment.GetCommandLineArgs().Length > 1)
         concat = delegate(string s1, string s2) { WriteToFile(s1, s2); };
      else
         concat = delegate(string s1, string s2) { WriteToConsole(s1, s2);} ;

      concat(message1, message2);
   }

   private static void WriteToConsole(string string1, string string2)
   {
      Console.WriteLine("{0}\n{1}", string1, string2);
   }

   private static void WriteToFile(string string1, string string2)
   {
      StreamWriter writer = null;
      try
      {
         writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
         writer.WriteLine("{0}\n{1}", string1, string2);
      }
      catch
      {
         Console.WriteLine("File write operation failed...");
      }
      finally
      {
         if (writer != null) writer.Close();
      }
   }
}

您也可以將 Lambda 運算式指派給 Action<T1,T2> 委派實例,如下列範例所示。 (如需 Lambda 運算式的簡介,請參閱 Lambda 運算式 (C#) Lambda 運算式 (F#) .)

C#
using System;
using System.IO;

public class TestLambdaExpression
{
   public static void Main()
   {
      string message1 = "The first line of a message.";
      string message2 = "The second line of a message.";
      Action<string, string> concat;

      if (Environment.GetCommandLineArgs().Length > 1)
         concat = (s1, s2) => WriteToFile(s1, s2);
      else
         concat = (s1, s2) => WriteToConsole(s1, s2);

      concat(message1, message2);
   }

   private static void WriteToConsole(string string1, string string2)
   {
      Console.WriteLine("{0}\n{1}", string1, string2);
   }

   private static void WriteToFile(string string1, string string2)
   {
      StreamWriter writer = null;
      try
      {
         writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
         writer.WriteLine("{0}\n{1}", string1, string2);
      }
      catch
      {
         Console.WriteLine("File write operation failed...");
      }
      finally
      {
         if (writer != null) writer.Close();
      }
   }
}

擴充方法

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 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

另請參閱