Updated: June 2010
Encapsulates a method that has no parameters and does not return a value.
Assembly: mscorlib (in mscorlib.dll)
Public Delegate Sub Action
public delegate void Action()
public delegate void Action()
type Action = delegate of unit -> unit
You can use this 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 no parameters and no return 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.
Note
|
|---|
|
To reference a method that has no parameters and returns a value, use the generic Func<TResult> delegate instead. |
When you use the Action delegate, you do not have to explicitly define a delegate that encapsulates a parameterless procedure. For example, the following code explicitly declares a delegate named ShowValue and assigns a reference to the Name.DisplayToWindow instance method to its delegate instance.
Public Delegate Sub ShowValue Public Class Name Private instanceName As String Public Sub New(name As String) Me.instanceName = name End Sub Public Sub DisplayToConsole() Console.WriteLine(Me.instanceName) End Sub Public Sub DisplayToWindow() MsgBox(Me.instanceName) End Sub End Class Public Module testDelegate Public Sub Main() Dim testName As New Name("Koani") Dim showMethod As ShowValue = AddressOf testName.DisplayToWindow showMethod End Sub End Module
using System; using System.Windows.Forms; public delegate void ShowValue(); public class Name { private string instanceName; public Name(string name) { this.instanceName = name; } public void DisplayToConsole() { Console.WriteLine(this.instanceName); } public void DisplayToWindow() { MessageBox.Show(this.instanceName); } } public class testTestDelegate { public static void Main() { Name testName = new Name("Koani"); ShowValue showMethod = testName.DisplayToWindow; showMethod(); } }
using namespace System; using namespace System::Windows::Forms; public delegate void ShowValue(); public ref class Name { private: String^ instanceName; public: Name(String^ name) { instanceName = name; } void DisplayToConsole() { Console::WriteLine(this->instanceName); } void DisplayToWindow() { MessageBox::Show(this->instanceName); } }; int main() { Name^ testName = gcnew Name(L"Koani"); ShowValue^ showMethod; showMethod = gcnew ShowValue(testName, &Name::DisplayToWindow); showMethod(); return 0; }
The following example simplifies this code by instantiating the Action delegate instead of explicitly defining a new delegate and assigning a named method to it.
Public Class Name Private instanceName As String Public Sub New(name As String) Me.instanceName = name End Sub Public Sub DisplayToConsole() Console.WriteLine(Me.instanceName) End Sub Public Sub DisplayToWindow() MsgBox(Me.instanceName) End Sub End Class Public Module testDelegate Public Sub Main() Dim testName As New Name("Koani") Dim showMethod As Action = AddressOf testName.DisplayToWindow showMethod End Sub End Module
using System; using System.Windows.Forms; public class Name { private string instanceName; public Name(string name) { this.instanceName = name; } public void DisplayToConsole() { Console.WriteLine(this.instanceName); } public void DisplayToWindow() { MessageBox.Show(this.instanceName); } } public class testTestDelegate { public static void Main() { Name testName = new Name("Koani"); Action showMethod = testName.DisplayToWindow; showMethod(); } }
#using <System.Windows.Forms.dll> using namespace System; using namespace System::Windows::Forms; public ref class Name { private: String^ instanceName; public: Name(String^ name) { instanceName = name; } void DisplayToConsole() { Console::WriteLine(this->instanceName); } void DisplayToWindow() { MessageBox::Show(this->instanceName); } }; int main() { Name^ testName = gcnew Name(L"Koani"); System::Action^ showMethod; showMethod += gcnew Action(testName, &Name::DisplayToWindow); showMethod(); return 0; }
You can also use the Action 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.Windows.Forms; public class Name { private string instanceName; public Name(string name) { this.instanceName = name; } public void DisplayToConsole() { Console.WriteLine(this.instanceName); } public void DisplayToWindow() { MessageBox.Show(this.instanceName); } } public class Anonymous { public static void Main() { Name testName = new Name("Koani"); Action showMethod = delegate() { testName.DisplayToWindow();} ; showMethod(); } }
You can also assign a lambda expression to an Action delegate instance, as the following example illustrates. (For an introduction to lambda expressions, see Lambda Expressions (C# Programming Guide).)
Public Class Name Private instanceName As String Public Sub New(name As String) Me.instanceName = name End Sub Public Function DisplayToConsole() As Integer Console.WriteLine(Me.instanceName) Return 0 End Function Public Function DisplayToWindow() As Integer Return MsgBox(Me.instanceName) End Function End Class Module LambdaExpression Public Sub Main() Dim name1 As New Name("Koani") Dim methodCall As Action = Sub() name1.DisplayToWindow() methodCall() End Sub End Module
using System; using System.Windows.Forms; public class Name { private string instanceName; public Name(string name) { this.instanceName = name; } public void DisplayToConsole() { Console.WriteLine(this.instanceName); } public void DisplayToWindow() { MessageBox.Show(this.instanceName); } } public class LambdaExpression { public static void Main() { Name testName = new Name("Koani"); Action showMethod = () => testName.DisplayToWindow(); showMethod(); } }
.NET Framework
Supported in: 4, 3.5.NET Framework Client Profile
Supported in: 4, 3.5 SP1Portable Class Library
Supported in: Portable Class LibraryWindows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Reference
|
Date |
History |
Reason |
|---|---|---|
|
June 2010 |
Modified Visual Basic lambda expression to use Sub keyword. |
Customer feedback. |
Here is an example using Action instead of an interface to perform reporting:
using System; using System.IO; namespace MyApplication { public class Program { static Action logToConsole = () => { Console.WriteLine(DateTime.Now); }; static Action logToFile = () => { File.AppendAllText(@"C:\temp\log.txt", DateTime.Now.ToString()); }; static void Main() { var worker1 = new Worker(); var worker2 = new Worker(); worker1.Work(logToConsole); worker2.Work(logToFile); } } public class Worker { public void Work(Action report) { // Do some work report(); // Do some more work report(); } } }
Here is an example using Action to wrap code in a measuring context:
using System; using System.Threading; namespace MyApplication { public class Program { static void Main() { var measurer = new Measurer(); measurer.Measure(() => Work(1000), true); } static void Work(int duration) { Thread.Sleep(duration); throw new TimeoutException(); } } public class Measurer { public void Measure(Action action, bool trapException) { DateTime start = DateTime.Now; try { action(); } catch (Exception ex) { if (trapException) Console.WriteLine("Action failed with a {0}: {1}", ex.GetType().Name, ex.Message); else throw; } Console.WriteLine("{0:f2} seconds", (DateTime.Now - start).TotalSeconds); } } }
Note