Action(Of T1, T2) Delegate
Updated: February 2009
Encapsulates a method that has two parameters and does not return a value.
Assembly: System.Core (in System.Core.dll)
'Declaration Public Delegate Sub Action(Of T1, T2) ( _ arg1 As T1, _ arg2 As T2 _ ) 'Usage Dim instance As New Action(Of T1, T2)(AddressOf HandlerMethod)
Type Parameters
- T1
The type of the first parameter of the method that this delegate encapsulates.
- T2
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.
You can use the Action(Of T1, T2) delegate to pass a method as a parameter without explicitly declaring a custom delegate. The 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 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.) Typically, such a method is used to perform an operation.
Note: |
|---|
To reference a method that has two parameters and returns a value, use the generic Func(Of T1, T2, TResult) delegate instead. |
When you use the Action(Of 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 file.
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
The following example simplifies this code by instantiating the Action(Of T1, T2) delegate rather than explicitly defining a new delegate and assigning a named method to it.
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
You can also use the Action(Of T1, T2) delegate with anonymous methods in C#, as the following example illustrates. (For an introduction to anonymous methods, see Anonymous Methods (C# Programming Guide).)
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(); } } }
You can also assign a lambda expression to an Action(Of T1, T2) delegate instance, as the following example illustrates. (For an introduction to lambda expressions, see Lambda Expressions (C# Programming Guide).)
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 = Function(s1, s2) WriteToFile(s1, s2) Else concat = Function(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
Note: |
|---|
Visual Basic requires that a lambda expression return a value. As a result, that return value must be discarded if the lambda expression is to be used with the Action(Of T1, T2) delegate. |
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: