Export (0) Print
Expand All
Expand Minimize

Action Generic Delegate

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

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

public delegate void Action<T> (
	T obj
)
J# supports the use of generic types and methods, but not the declaration of new ones.
JScript does not support generic types and methods.
Not applicable.

Type Parameters

T

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

Parameters

obj

The parameter of the method that this delegate encapsulates.

You can use this 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 one parameter that is 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 SubEnd Sub construct.) 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 delegate instead.

When you use the Action 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.

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

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).)

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).)

NoteNote:

Visual Basic requires that a lambda expression return a value. As a result, the Action delegate cannot be used with a lambda expression in Visual Basic.

The ForEach and ForEach methods each take an Action 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.

The following example demonstrates the use of the Action delegate to print the contents of a List 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.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<String> names = new List<String>();
        names.Add("Bruce");
        names.Add("Alfred");
        names.Add("Tim");
        names.Add("Richard");

        // Display the contents of the List out using the "Print" delegate.
        names.ForEach(Print);

        // The following demonstrates the Anonymous Delegate feature of C#
        // to display the contents of the List to the console.
        names.ForEach(delegate(String name)
        {
            Console.WriteLine(name);
        });
    }

    private static void Print(string s)
    {
        Console.WriteLine(s);
    }
}
/* This code will produce output similar to the following:
 * Bruce
 * Alfred
 * Tim
 * Richard
 * Bruce
 * Alfred
 * Tim
 * Richard
 */

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft