Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Action — a delegate with no return value

System.Action represents a parameterless operation that returns void. The Action<T> variant accepts one argument. Both types let you store and pass behavior, such as a method that prints a message, without declaring a custom delegate type.

Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification

Syntax

System.Action action = () => { /* operation */ };
System.Action<string> consume = value => { /* use value */ };
action();
consume("Hello");

System.Action is a delegate type representing a method with no parameters and no return value (void). Action is its short name available after using System;. A delegate lets you store an operation in a variable and invoke it later or pass it to other code as a callback.

Action<T> is a separate, generic delegate type: it accepts one argument of type T and also returns void. The type T describes the argument, not a result. If the operation must return a value, use an appropriate Func type, such as Func<string, int> instead of Action<string>.

You can assign a compatible method by name or use a lambda expression. Assignment alone does not execute the operation. In the example, write refers to Console.WriteLine, while greet stores a parameterless lambda. Only greet() prints Hello, Ada!; the subsequent call write("Done.") prints Done..

Examples

using System;

internal static class Program
{
    private static void Main()
    {
        Action<string> write = Console.WriteLine;
        Action greet = () => write("Hello, Ada!");

        greet();
        write("Done.");
    }
}

Result

Hello, Ada!
Done.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources