Condividi tramite


Action<T1, T2> Delegate

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: October 2010

Encapsulates a method that has two parameters and does not return a value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<TypeForwardedFromAttribute("System.Core, Version=2.0.5.0, Culture=Neutral, PublicKeyToken=7cec85d7bea7798e")> _
Public Delegate Sub Action(Of In T1, In T2) ( _
    arg1 As T1, _
    arg2 As T2 _
)
[TypeForwardedFromAttribute("System.Core, Version=2.0.5.0, Culture=Neutral, PublicKeyToken=7cec85d7bea7798e")]
public delegate void Action<in T1, in T2>(
    T1 arg1,
    T2 arg2
)

Type Parameters

  • inT1
    The type of the first parameter of the method that this delegate encapsulates.

    This type parameter is contravariant. That is, you can use either the type you specified or any type that is less derived. For more information about covariance and contravariance, see 2678dc63-c7f9-4590-9ddc-0a4df684d42e.

  • inT2
    The type of the second parameter of the method that this delegate encapsulates.

Parameters

  • arg1
    Type: T1
    The first parameter of the method that this delegate encapsulates.
  • arg2
    Type: T2
    The second parameter of the method that this delegate encapsulates.

Remarks

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. 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. (In C#, the method must return void. In Visual Basic, it must be defined by the Sub…End 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.

NoteNote:

To reference a method that has two parameters and returns a value, use the generic Func<T1, T2, TResult> delegate instead.

When you use the Action<T1, T2> delegate, you do not have to explicitly define a delegate that encapsulates a method with two parameters. For example, the following code explicitly declares a delegate named ConcatStrings. It then assigns a reference to either of two methods to its delegate instance. One method writes two strings to the console; the second writes two strings to a message box.

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

Module Example
   Private outputBlock As System.Windows.Controls.TextBlock

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock

      Dim message1 As String = "The first line of a message."
      Dim message2 As String = "The second line of a message."
      Dim concat As ConcatStrings

      ' Generate a random number and use it to determine which method is
      ' assigned to the delegate;
      Dim rnd As New Random()
      If rnd.NextDouble() <= .5 Then 
         concat = AddressOf WriteToWindow
      Else
         concat = AddressOf WriteToConsole
      End If
      concat(message1, message2)
   End Sub

   Private Sub WriteToConsole(ByVal string1 As String, ByVal string2 As String)
      outputBlock.Text += String.Format("{0}{1}{2}", string1, vbCrLf, string2) & vbCrLf
   End Sub

   Private Sub WriteToWindow(ByVal string1 As String, ByVal string2 As String)
      System.Windows.Browser.HtmlPage.Window.Alert(string.Format("{0}{1}{2}", _ 
                                                   string1, vbCrLf, string2))
   End Sub
End Module
using System;

delegate void ConcatStrings(string string1, string string2);

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outBlock)
   {
      outputBlock = outBlock;

      string message1 = "The first line of a message.";
      string message2 = "The second line of a message.";
      ConcatStrings concat;

      // Generate a random number and use it to determine which method is
      // assigned to the delegate;
      Random rnd = new Random();
      if (rnd.NextDouble() <= .5) 
         concat = WriteToWindow;
      else
         concat = WriteToConsole;

      concat(message1, message2);
   }

   private static void WriteToConsole(string string1, string string2)
   {
      outputBlock.Text += String.Format("{0}\n{1}", string1, string2) + "\n";
   }

   private static void WriteToWindow(string string1, string string2)
   {
      System.Windows.Browser.HtmlPage.Window.Alert(string.Format("{0}\n{1}", 
                                                   string1, string2));
   }
}

The following example simplifies this code by instantiating the Action<T1, T2> delegate rather than explicitly defining a new delegate and assigning a named method to it.

Module Example
   Private outputBlock As System.Windows.Controls.TextBlock

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock

      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)

      ' Generate a random number and use it to determine which method is
      ' assigned to the delegate;
      Dim rnd As New Random()
      If rnd.NextDouble() <= .5 Then 
         concat = AddressOf WriteToWindow
      Else
         concat = AddressOf WriteToConsole
      End If
      concat(message1, message2)
   End Sub

   Private Sub WriteToConsole(ByVal string1 As String, ByVal string2 As String)
      outputBlock.Text += String.Format("{0}{1}{2}", string1, vbCrLf, string2) & vbCrLf
   End Sub

   Private Sub WriteToWindow(ByVal string1 As String, ByVal string2 As String)
      System.Windows.Browser.HtmlPage.Window.Alert(String.Format("{0}{1}{2}", _
                                                   string1, vbCrLf, string2))
   End Sub
End Module
using System;

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outBlock)
   {
      outputBlock = outBlock;
      string message1 = "The first line of a message.";
      string message2 = "The second line of a message.";
      Action<string, string> concat;

      // Generate a random number and use it to determine which method is
      // assigned to the delegate;
      Random rnd = new Random();
      if (rnd.NextDouble() <= .5) 
         concat = WriteToWindow;
      else
         concat = WriteToConsole;

      concat(message1, message2);
   }

   private static void WriteToConsole(string string1, string string2)
   {
      outputBlock.Text += String.Format("{0}\n{1}\n", string1, string2);
   }

   private static void WriteToWindow(string string1, string string2)
   {
      System.Windows.Browser.HtmlPage.Window.Alert(String.Format("{0}\n{1}\n", 
                                                   string1, string2));
   }
}

You can also use the Action<T1, T2> delegate with anonymous methods in C#, as the following example illustrates.

using System;

public class TestAnonymousMethod
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outBlock)
   {
      outputBlock = outBlock;

      string message1 = "The first line of a message.";
      string message2 = "The second line of a message.";
      Action<string, string> concat;

      // Generate a random number and use it to determine which method is
      // assigned to the delegate;
      Random rnd = new Random();
      if (rnd.NextDouble() <= .5) 
         concat = delegate(string s1, string s2) { WriteToWindow(s1, s2); };
      else
         concat = delegate(string s1, string s2) { WriteToConsole(s1, s2); };

      concat(message1, message2);
   }

   private static void WriteToConsole(string string1, string string2)
   {
      outputBlock.Text += String.Format("{0}\n{1}", string1, string2) + "\n";
   }

   private static void WriteToWindow(string string1, string string2)
   {
      System.Windows.Browser.HtmlPage.Window.Alert(string.Format("{0}\n{1}", 
                                                   string1, string2));
   }
}

You can also assign a lambda expression to an Action<T1, T2> delegate instance, as the following example illustrates.

Module Example
   Private outputBlock As System.Windows.Controls.TextBlock

   Public Sub Demo(outBlock As System.Windows.Controls.TextBlock)
      outputBlock = outBlock

      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)

      ' Generate a random number and use it to determine which method is
      ' assigned to the delegate;
      Dim rnd = New Random()
      If rnd.NextDouble() <= .5 Then 
         concat = Sub(s1, s2) WriteToWindow(s1, s2)
      Else
         concat = Sub(s1, s2) WriteToBrowser(s1, s2)
      End If
      concat(message1, message2)
    End Sub

   Private Sub WriteToBrowser(string1 As String, string2 As String)
      outputBlock.Text += String.Format("{0}{2}{1}", string1, string2, vbCrLf)
   End Sub

   Private Sub WriteToWindow(string1 As String, string2 As String)
      System.Windows.Browser.HtmlPage.Window.Alert(string.Format("{0}{2}{1}", 
                                                   string1, string2, vbCrLf))
   End Sub
End Module
using System;

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outBlock)
   {
      outputBlock = outBlock;

      string message1 = "The first line of a message.";
      string message2 = "The second line of a message.";
      Action<string, string> concat;

      // Generate a random number and use it to determine which method is
      // assigned to the delegate;
      Random rnd = new Random();
      if (rnd.NextDouble() <= .5) 
         concat = (s1, s2) => WriteToWindow(s1, s2);
      else
         concat = (s1, s2) => WriteToConsole(s1, s2);

      concat(message1, message2);
   }

   private static void WriteToConsole(string string1, string string2)
   {
      outputBlock.Text += String.Format("{0}\n{1}", string1, string2) + "\n";
   }

   private static void WriteToWindow(string string1, string string2)
   {
      System.Windows.Browser.HtmlPage.Window.Alert(string.Format("{0}\n{1}", 
                                                   string1, string2));
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Change History

Date

History

Reason

October 2010

Modified Visual Basic lambda expression to use Sub keyword.

Customer feedback.