英語で読む

次の方法で共有


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

このデリゲートによってカプセル化されるメソッドの 2 番目のパラメーターの型。

この型パラメーターは反変です。 つまり、指定した型、または弱い派生型のいずれかを使用することができます。 共変性および反変性の詳細については、「ジェネリックの共変性と反変性」をご覧ください。

パラメーター

arg1
T1

このデリゲートによってカプセル化されるメソッドの最初のパラメーター。

arg2
T2

このデリゲートによってカプセル化されるメソッドの 2 番目のパラメーター。

注釈

デリゲートを Action<T1,T2> 使用すると、カスタム デリゲートを明示的に宣言せずに、メソッドをパラメーターとして渡すことができます。 カプセル化されたメソッドは、このデリゲートによって定義されるメソッド シグネチャに対応している必要があります。 つまり、カプセル化されたメソッドには、両方とも値渡しされる 2 つのパラメーターが必要であり、値を返してはなりません。 (C# では、メソッド voidは . F# では、メソッドまたは関数は単位を返す必要があります。 Visual Basicでは、... で定義するSub必要があります。End Sub construct。 また、無視される値を返すメソッドを指定することもできます)。通常、このようなメソッドを使用して操作を実行します。

注意

2 つのパラメーターを持ち、値を返すメソッドを参照するには、代わりにジェネリック Func<T1,T2,TResult> デリゲートを使用します。

デリゲートを Action<T1,T2> 使用する場合、2 つのパラメーターを持つメソッドをカプセル化するデリゲートを明示的に定義する必要はありません。 たとえば、次のコードでは、という名前 ConcatStringsのデリゲートを明示的に宣言しています。 次に、デリゲート インスタンスに 2 つのメソッドのいずれかに参照を割り当てます。 1 つのメソッドがコンソールに 2 つの文字列を書き込みます。2 つ目は、2 つの文字列をファイルに書き込みます。

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# の匿名メソッドでデリゲートを使用することもできます。 (匿名メソッドの概要については、「 匿名メソッド」を参照してください)。

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

次の例に示すように、ラムダ式を Action<T1,T2> デリゲート インスタンスに割り当てることもできます。 (ラムダ式の概要については、 ラムダ式 (C#) または ラムダ式 (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

こちらもご覧ください