System Namespace


.NET Framework Class Library for Silverlight
Action<(Of <(T>)>) Delegate

Encapsulates a method that takes a single parameter and does not return a value.

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

Visual Basic (Declaration)
Public Delegate Sub Action(Of T) ( _
    obj As T _
)
Visual Basic (Usage)
Dim instance As New Action(Of T)(AddressOf HandlerMethod)
C#
public delegate void Action<T>(
    T obj
)

Type Parameters

T

The type of the parameter of the method that this delegate encapsulates.

Parameters

obj
Type: T
The parameter of the method that this delegate encapsulates.
Remarks

You can use the Action<(Of <(T>)>) 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 one parameter that is 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 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.

NoteNote:

To reference a method that has one parameter and returns a value, use the generic Func<(Of <(T, TResult>)>) delegate instead.

When you use the Action<(Of <(T>)>) delegate, you do not have to explicitly define a delegate that encapsulates a method with a single parameter. For example, the following code explicitly declares a delegate named DisplayMessage and assigns a reference to either the WriteLine method or the ShowWindowsMessage method to its delegate instance.

Visual Basic
Delegate Sub DisplayMessage(message As String) 

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

   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Example.outputBlock = outputBlock 
      Dim messageTarget As DisplayMessage 

      Dim rnd As New Random()
      If rnd.NextDouble() <= .5
         messageTarget = AddressOf ShowAlertMessage
      Else
         messageTarget = AddressOf ShowBrowserMessage
      End If
      messageTarget("Hello, World!")
   End Sub

   Private Sub ShowAlertMessage(message As String)
      System.Windows.Browser.HtmlPage.Window.Alert(message)
   End Sub   

   Private Sub ShowBrowserMessage(message As String)
      outputBlock.Text += message + vbCrLf
   End Sub
End Module
C#
using System;

delegate void DisplayMessage(string message);

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

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;
      DisplayMessage messageTarget; 

      Random rnd = new Random();
      if (rnd.NextDouble() <= .5)
         messageTarget = ShowAlertMessage;
      else
         messageTarget = ShowBrowserMessage;

      messageTarget("Hello, World!");   
   }      

   private static void ShowAlertMessage(string message)
   {
      System.Windows.Browser.HtmlPage.Window.Alert(message);      
   }

   private static void ShowBrowserMessage(string message)
   {
      outputBlock.Text += message + "\n";
   }
}

The following example simplifies this code by instantiating the Action<(Of <(T>)>) delegate rather than explicitly defining a new delegate and assigning a named method to it.

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

   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Example.outputBlock = outputBlock
      Dim messageTarget As Action(Of String) 

      Dim rnd As New Random()
      If rnd.NextDouble() <= .5
         messageTarget = AddressOf ShowAlertMessage
      Else
         messageTarget = AddressOf ShowBrowserMessage
      End If
      messageTarget("Hello, World!")
   End Sub

   Private Sub ShowAlertMessage(message As String)
      System.Windows.Browser.HtmlPage.Window.Alert(message)
   End Sub   

   Private Sub ShowBrowserMessage(message As String)
      outputBlock.Text += message + vbCrLf
   End Sub
End Module
C#
using System;

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

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;
      Action<string> messageTarget; 

      Random rnd = new Random();
      if (rnd.NextDouble() <= .5)
         messageTarget = ShowAlertMessage;
      else
         messageTarget = ShowBrowserMessage;

      messageTarget("Hello, World!");   
   }      

   private static void ShowAlertMessage(string message)
   {
      System.Windows.Browser.HtmlPage.Window.Alert(message);      
   }

   private static void ShowBrowserMessage(string message)
   {
      outputBlock.Text += message + "\n";
   }
}

You can also use the Action<(Of <(T>)>) delegate with anonymous methods in C#, as the following example illustrates.

C#
using System;

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

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;
      Action<string> messageTarget; 

      Random rnd = new Random();
      if (rnd.NextDouble() <= .5)
         messageTarget = delegate(string s) { ShowAlertMessage(s); };
      else
         messageTarget = delegate(string s) { ShowBrowserMessage(s); };

      messageTarget("Hello, World!");
   }

   private static void ShowAlertMessage(string message)
   {
      System.Windows.Browser.HtmlPage.Window.Alert(message);      
   }

   private static void ShowBrowserMessage(string message)
   {
      outputBlock.Text += message + "\n";
   }
}

You can also assign a lambda expression to an Action<(Of <(T>)>) delegate instance, as the following example illustrates.

C#
using System;

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

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;
      Action<string> messageTarget; 

      Random rnd = new Random();
      if (rnd.NextDouble() <= .5)
         messageTarget = s => ShowAlertMessage(s); 
      else
         messageTarget = s => ShowBrowserMessage(s);

      messageTarget("Hello, World!");
   }

   private static void ShowAlertMessage(string message)
   {
      System.Windows.Browser.HtmlPage.Window.Alert(message);      
   }
   private static void ShowBrowserMessage(string message)
   {
      outputBlock.Text += message + "\n";
   }
}
NoteNote:

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 <(T>)>) delegate.

The ForEach and ForEach<(Of <(T>)>) methods each take an Action<(Of <(T>)>) delegate as a parameter. The method encapsulated by the delegate allows you to perform an action on each element in the array or list. The example uses the ForEach method to provide an illustration.

Examples

The following example demonstrates the use of the Action<(Of <(T>)>) delegate to print the contents of a List<(Of <(T>)>) object. In this example, the Print 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<(Of <(T>)>) variable. Instead, it passes a reference to a method that takes a single parameter and that does not return a value to the List<(Of <(T>)>)..::.ForEach method, whose single parameter is an Action<(Of <(T>)>) delegate. Similarly, in the C# example, an Action<(Of <(T>)>) delegate is not explicitly instantiated because the signature of the anonymous method matches the signature of the Action<(Of <(T>)>) delegate that is expected by the List<(Of <(T>)>)..::.ForEach method.

Visual Basic
Imports System.Collections.Generic

Class Example
   Private Shared outputBlock As System.Windows.Controls.TextBlock
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock
      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)
      outputBlock.Text &= s & vbCrLf
   End Sub
End Class

' This code will produce output similar to the following:
' Bruce
' Alfred
' Tim
' Richard
C#
using System;
using System.Collections.Generic;

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

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

      Example.outputBlock = outputBlock;
      List<String> names = new List<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(Print);

      // The following demonstrates the anonymous method feature of C#
      // to display the contents of the list.
      names.ForEach(delegate(String name)
      {
         outputBlock.Text += name + "\n";
      });
   }

   private static void Print(string s)
   {
      outputBlock.Text += s + "\n";
   }
}
/* This code will produce output similar to the following:
 * Bruce
 * Alfred
 * Tim
 * Richard
 * Bruce
 * Alfred
 * Tim
 * Richard
 */
Platforms

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

See Also

Reference

Tags :


Page view tracker