Action Delegate
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Encapsulates a method that has a single parameter and does not return a value.
generic
public delegate void Action(T obj);
public delegate void Action(T obj);
public delegate void Action(T obj) where T : allows ref struct;
public delegate void Action(T obj);
type Action<'T> = delegate of 'T -> unit
Public Delegate Sub Action(Of In T)(obj As T)
Public Delegate Sub Action(Of T)(obj As T)
Type Parameters
- T
The type of the 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 Covariance and Contravariance in Generics.Parameters
- obj
- T
The parameter of the method that this delegate encapsulates.
Examples
The following example demonstrates the use of the ActionPrint method is used to display the contents of the list to the console. In addition, the C# example also demonstrates the use of anonymous methods to display the contents to the console. Note that the example does not explicitly declare an Action
List names = new List();
names.Add("Bruce");
names.Add("Alfred");
names.Add("Tim");
names.Add("Richard");
// Display the contents of the list using the Print method.
names.ForEach(Print);
// The following demonstrates the anonymous method feature of C#
// to display the contents of the list to the console.
names.ForEach(delegate(string name)
{
Console.WriteLine(name);
});
void Print(string s)
{
Console.WriteLine(s);
}
/* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*/
// F# provides a type alias for System.Collections.List<'T> as ResizeArray<'T>.
let names = ResizeArray()
names.Add "Bruce"
names.Add "Alfred"
names.Add "Tim"
names.Add "Richard"
let print s = printfn "%s" s
// Display the contents of the list using the print function.
names.ForEach(Action print)
// The following demonstrates the lambda expression feature of F#
// to display the contents of the list to the console.
names.ForEach(fun s -> printfn "%s" s)
(* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*)
Imports System.Collections.Generic
Class Program
Shared Sub Main()
Dim names As New List(Of String)
names.Add("Bruce")
names.Add("Alfred")
names.Add("Tim")
names.Add("Richard")
' Display the contents of the list using the Print method.
names.ForEach(AddressOf Print)
End Sub
Shared Sub Print(ByVal s As String)
Console.WriteLine(s)
End Sub
End Class
' This code will produce output similar to the following:
' Bruce
' Alfred
' Tim
' Richard
Remarks
You can use the Actionvoid. 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.
Note
To reference a method that has one parameter and returns a value, use the generic Func
When you use the ActionDisplayMessage and assigns a reference to either the WriteLine method or the ShowWindowsMessage method to its delegate instance.
using System;
using System.Windows.Forms;
delegate void DisplayMessage(string message);
public class TestCustomDelegate
{
public static void Main()
{
DisplayMessage messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
open System
open System.Windows.Forms
type DisplayMessage = delegate of message: string -> unit
let showWindowsMessage message =
MessageBox.Show message |> ignore
let messageTarget =
DisplayMessage(
if Environment.GetCommandLineArgs().Length > 1 then
showWindowsMessage
else
printfn "%s"
)
messageTarget.Invoke "Hello, World!"
Delegate Sub DisplayMessage(message As String)
Module TestCustomDelegate
Public Sub Main
Dim messageTarget As DisplayMessage
If Environment.GetCommandLineArgs().Length > 1 Then
messageTarget = AddressOf ShowWindowsMessage
Else
messageTarget = AddressOf Console.WriteLine
End If
messageTarget("Hello, World!")
End Sub
Private Sub ShowWindowsMessage(message As String)
MsgBox(message)
End Sub
End Module
The following example simplifies this code by instantiating the Action
using System;
using System.Windows.Forms;
public class TestAction1
{
public static void Main()
{
Action messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
open System
open System.Windows.Forms
let showWindowsMessage message =
MessageBox.Show message |> ignore
let messageTarget =
Action(
if Environment.GetCommandLineArgs().Length > 1 then
showWindowsMessage
else
printfn "%s"
)
messageTarget.Invoke "Hello, World!"
Module TestAction1
Public Sub Main
Dim messageTarget As Action(Of String)
If Environment.GetCommandLineArgs().Length > 1 Then
messageTarget = AddressOf ShowWindowsMessage
Else
messageTarget = AddressOf Console.WriteLine
End If
messageTarget("Hello, World!")
End Sub
Private Sub ShowWindowsMessage(message As String)
MsgBox(message)
End Sub
End Module
You can also use the Action
using System;
using System.Windows.Forms;
public class TestAnonMethod
{
public static void Main()
{
Action messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = delegate(string s) { ShowWindowsMessage(s); };
else
messageTarget = delegate(string s) { Console.WriteLine(s); };
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
You can also assign a lambda expression to an Action
using System;
using System.Windows.Forms;
public class TestLambdaExpression
{
public static void Main()
{
Action messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = s => ShowWindowsMessage(s);
else
messageTarget = s => Console.WriteLine(s);
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
open System
open System.Windows.Forms
let showWindowsMessage message =
MessageBox.Show message |> ignore
let messageTarget =
Action(
if Environment.GetCommandLineArgs().Length > 1 then
fun s -> showWindowsMessage s
else
fun s -> printfn "%s" s
)
messageTarget.Invoke "Hello, World!"
Imports System.Windows.Forms
Public Module TestLambdaExpression
Public Sub Main()
Dim messageTarget As Action(Of String)
If Environment.GetCommandLineArgs().Length > 1 Then
messageTarget = Sub(s) ShowWindowsMessage(s)
Else
messageTarget = Sub(s) ShowConsoleMessage(s)
End If
messageTarget("Hello, World!")
End Sub
Private Function ShowWindowsMessage(message As String) As Integer
Return MessageBox.Show(message)
End Function
Private Function ShowConsoleMessage(message As String) As Integer
Console.WriteLine(message)
Return 0
End Function
End Module
The ForEach and ForEach methods each take an Action
Extension Methods
| Name | Description |
|---|---|
| GetMethodInfo(Delegate) |
Gets an object that represents the method represented by the specified delegate. |