Action<T1,T2> Action<T1,T2> Action<T1,T2> Action<T1,T2> Delegate

定義

2 つのパラメーターを受け取り、戻り値を持たないメソッドをカプセル化します。Encapsulates a method that has two parameters and does not return a value.

generic <typename T1, typename T2>
public delegate void Action(T1 arg1, T2 arg2);
public delegate void Action<in T1,in T2>(T1 arg1, T2 arg2);
type Action<'T1, 'T2> = delegate of 'T1 * 'T2 -> unit
Public Delegate Sub Action(Of In T1, In T2)(arg1 As T1, arg2 As T2)

型パラメーター

T1

このデリゲートによってカプセル化されるメソッドの最初のパラメーターの型。The type of the first parameter of the method that this delegate encapsulates.

T2

このデリゲートによってカプセル化されるメソッドの第 2 パラメーターの型。The type of the second parameter of the method that this delegate encapsulates.

パラメーター

arg1

このデリゲートによってカプセル化されるメソッドの最初のパラメーター。The first parameter of the method that this delegate encapsulates.

arg2

このデリゲートによってカプセル化されるメソッドの 2 番目のパラメーター。The second parameter of the method that this delegate encapsulates.

継承
Action<T1,T2>Action<T1,T2>Action<T1,T2>Action<T1,T2>

注釈

使用することができます、Action<T1,T2>カスタム デリゲートを明示的に宣言することがなく、パラメーターとして、メソッドに渡すデリゲート。You can use the Action<T1,T2> delegate to pass a method as a parameter without explicitly declaring a custom delegate. カプセル化されるメソッドは、このデリゲートで定義されているメソッド シグネチャに対応する必要があります。The encapsulated method must correspond to the method signature that is defined by this delegate. これは、カプセル化されるメソッドは、値を渡し、2 つのパラメーターが必要し、値を返す必要がないことを意味します。This means that the encapsulated method must have two parameters that are both passed to it by value, and it must not return a value. (C# のメソッドが返す必要がありますvoidします。(In C#, the method must return void. Visual basic で定義する必要があります、 Sub.End SubIn Visual Basic, it must be defined by the SubEnd Sub 構築します。construct. これは、ことができますが無視される値を返すメソッド。)通常、このようなメソッドは、操作の実行に使用されます。It can also be a method that returns a value that is ignored.) Typically, such a method is used to perform an operation.

注意

2 つのパラメーターを持つ値を返すメソッドを参照するには、ジェネリックを使用Func<T1,T2,TResult>代わりにデリゲートします。To reference a method that has two parameters and returns a value, use the generic Func<T1,T2,TResult> delegate instead.

使用すると、Action<T1,T2>デリゲートがありませんを明示的に 2 つのパラメーターを持つメソッドをカプセル化するデリゲートを定義します。When you use the Action<T1,T2> delegate, you do not have to explicitly define a delegate that encapsulates a method with two parameters. たとえば、次のコードで明示的にという名前のデリゲートが宣言ConcatStringsします。For example, the following code explicitly declares a delegate named ConcatStrings. これは、後、デリゲート インスタンスに 2 つのメソッドのいずれかへの参照を割り当てます。It then assigns a reference to either of two methods to its delegate instance. 1 つのメソッドは、コンソールに 2 つの文字列を書き込みます2 つ目は、2 つの文字列をファイルに書き込みます。One method writes two strings to the console; the second writes two strings to a file.

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();
      }      
   }
}
Imports System.IO

Delegate Sub ConcatStrings(string1 As String, string2 As String)

Module TestDelegate
   Public Sub Main()
      
      Dim message1 As String = "The first line of a message."
      Dim message2 As String = "The second line of a message."
      Dim concat As ConcatStrings
      
      If Environment.GetCommandLineArgs().Length > 1 Then
         concat = AddressOf WriteToFile
      Else
         concat = AddressOf WriteToConsole
      End If   
      concat(message1, message2)         
   End Sub
   
   Private Sub WriteToConsole(string1 As String, string2 As String)
      Console.WriteLine("{0}{1}{2}", string1, vbCrLf, string2)
   End Sub
   
   Private Sub WriteToFile(string1 As String, string2 As String)
      Dim writer As StreamWriter = Nothing  
      Try
         writer = New StreamWriter(Environment.GetCommandLineArgs(1), False)
         writer.WriteLine("{0}{1}{2}", string1, vbCrLf, string2)
      Catch
         Console.WriteLine("File write operation failed...")
      Finally
         If writer IsNot Nothing Then writer.Close
      End Try      
   End Sub
End Module

次の例では、インスタンス化してこのコードを簡略化、Action<T1,T2>明示的に新しいデリゲートを定義して、名前付きメソッドをそこに割り当てる代わりにデリゲートします。The following example simplifies this code by instantiating the Action<T1,T2> delegate instead of explicitly defining a new delegate and assigning a named method to it.

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();
      }      
   }
}
Imports System.IO

Module TestAction2
   Public Sub Main()
      
      Dim message1 As String = "The first line of a message."
      Dim message2 As String = "The second line of a message."
      Dim concat As Action(Of String, String)
      
      If Environment.GetCommandLineArgs().Length > 1 Then
         concat = AddressOf WriteToFile
      Else
         concat = AddressOf WriteToConsole
      End If   
      concat(message1, message2)         
   End Sub
   
   Private Sub WriteToConsole(string1 As String, string2 As String)
      Console.WriteLine("{0}{1}{2}", string1, vbCrLf, string2)
   End Sub
   
   Private Sub WriteToFile(string1 As String, string2 As String)
      Dim writer As StreamWriter = Nothing  
      Try
         writer = New StreamWriter(Environment.GetCommandLineArgs(1), False)
         writer.WriteLine("{0}{1}{2}", string1, vbCrLf, string2)
      Catch
         Console.WriteLine("File write operation failed...")
      Finally
         If writer IsNot Nothing Then writer.Close
      End Try      
   End Sub
End Module

使用することも、Action<T1,T2>次の例に示すように、c# の場合は、匿名メソッドを持つデリゲートします。You can also use the Action<T1,T2> delegate with anonymous methods in C#, as the following example illustrates. (匿名メソッドの概要については、次を参照してください匿名メソッド。)。(For an introduction to anonymous methods, see Anonymous Methods.)

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>次の例に示すように、インスタンスを委任します。You can also assign a lambda expression to an Action<T1,T2> delegate instance, as the following example illustrates. (ラムダ式の概要については、次を参照してくださいラムダ式。)。(For an introduction to lambda expressions, see Lambda Expressions.)

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();
      }      
   }
}
Imports System.IO

Public Module TestLambdaExpression

   Public Sub Main()
      Dim message1 As String = "The first line of a message."
      Dim message2 As String = "The second line of a message."
      Dim concat As Action(Of String, String)
      
      If Environment.GetCommandLineArgs().Length > 1 Then
         concat = Sub(s1, s2) WriteToFile(s1, s2)
      Else
         concat = Sub(s1, s2) WriteToConsole(s1, s2)
      End If
         
      concat(message1, message2)
   End Sub
  
   Private Function WriteToConsole(string1 As String, string2 As String) As Integer
      Dim message As String = String.Format("{0}{1}{2}", string1, vbCrLf, string2)
      Console.WriteLine(message)
      Return message.Length
   End Function

   Private Function WriteToFile(string1 As String, string2 As String) As Integer
      Dim writer As StreamWriter = Nothing  
      Dim message As String = String.Format("{0}{1}{2}", string1, vbCrLf, string2)
      Dim charsWritten As Integer
      Try
         writer = New StreamWriter(Environment.GetCommandLineArgs()(1), False)
         writer.WriteLine(message)
      Catch
         Console.WriteLine("File write operation failed...")
      Finally
         If writer IsNot Nothing Then 
            writer.Close()
            charsWritten = message.Length
         Else
            charsWritten = 0
         End If
      End Try      
      Return charsWritten
   End Function
End Module

Extension Methods

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。Gets an object that represents the method represented by the specified delegate.

適用対象

こちらもご覧ください