ZetCode

C# Action and Action<T>

last modified November 15, 2026

This article shows how to use the Action and Action<T> delegates in C#. We create actions from lambdas and method groups, pass callbacks to methods, combine several actions into one multicast delegate, and compare Action with Func<Task> for asynchronous work.

Action is a predefined delegate that represents a method without parameters and without a return value. The generic overloads Action<T>, Action<T1, T2>, and so on represent methods that take arguments and still return void. The family covers up to sixteen parameters, which is enough for practically every callback signature.

The name says what the delegate is for: an action is something to do, not a value to compute. When the callback has to return a result, use Func<TResult> instead; when it has to return true or false, Predicate<T> expresses that intent. Using the predefined delegates saves a custom delegate declaration for every callback.

Declaring an Action

An Action variable can be assigned a lambda, an anonymous method, or a method group. The compiler converts the method or lambda to the delegate type, checking the parameter types and the void return as it does for any other assignment.

Program.cs
Action greet = () => Console.WriteLine("Hello");
Action<string> write = Console.WriteLine;
Action<string, int> repeat = (text, count) =>
{
    for (int i = 0; i < count; i++)
    {
        Console.WriteLine(text);
    }
};

greet();
write("direct method group");
repeat("tick", 2);

greet takes no arguments, write is the Console.WriteLine(string) method group, and repeat takes two arguments. All three return void, which is what Action requires.

Action<string> write = Console.WriteLine;

A method group is converted to the delegate by matching its signature. Console.WriteLine has many overloads, and the compiler selects the one that takes a single string. If the selected overload returned a value, the assignment would not compile, because an Action cannot discard a result implicitly in a method group conversion.

$ dotnet run
Hello
direct method group
tick
tick

Passing an Action to a method

An Action parameter lets a method accept a piece of behavior without knowing what that behavior does. The method controls when and how often the action runs; the caller decides what happens. This is the caller-supplied loop body, the visitor callback, and the progress reporter in one shape.

Program.cs
int[] values = [1, 2, 3, 4];

Process(values, n => Console.WriteLine(n * n));

static void Process(int[] numbers, Action<int> action)
{
    foreach (int number in numbers)
    {
        action(number);
    }
}

Process iterates over the array and calls the action for each element. The same method can print squares, collect values, or write to a log, depending on the delegate it receives.

static void Process(int[] numbers, Action<int> action)
{
    foreach (int number in numbers)
    {
        action(number);
    }
}

The parameter type Action<int> fixes both the argument type and the return type. A lambda whose body produces a value, such as n => n * n, is not accepted; the body must be a statement expression or a void-returning call such as Console.WriteLine(n * n). Neither would a method that takes a string be accepted. Inside Process, the action is invoked like a method; the delegate's Invoke method is called behind the scenes.

$ dotnet run
1
4
9
16

Combining actions

Delegates support the + and - operators. Combining two actions produces a multicast delegate that invokes both, in the order in which they were combined. Removing an action takes the matching delegate out of the list. The same rules apply to Action as to any other delegate type.

Program.cs
Action<string> console = message => Console.WriteLine($"console: {message}");
Action<string> file = message => Console.WriteLine($"file: {message}");

Action<string>? all = console + file;
all?.Invoke("ready");
Console.WriteLine($"handlers: {all?.GetInvocationList().Length}");

all -= file;
all?.Invoke("done");
Console.WriteLine($"handlers: {all?.GetInvocationList().Length}");

The combined action writes to the console and to a file. After the removal, only the console handler remains.

Action<string>? all = console + file;
all?.Invoke("ready");

The combined delegate is nullable because - can reduce the list to nothing and produce null. Invoking through ?. keeps that case safe. GetInvocationList returns the individual delegates, which is also how an event publisher can invoke handlers one by one.

$ dotnet run
console: ready
file: ready
handlers: 2
console: done
handlers: 1

Action and asynchronous work

Action returns void, so an async lambda assigned to it becomes async void. The caller starts it and continues immediately; there is no task to await, and an exception thrown after the first suspension cannot be caught by the caller. When the caller needs to wait for the result, use Func<Task> instead.

Program.cs
Func<Task> save = async () =>
{
    await Task.Delay(10);
    Console.WriteLine("saved");
};

await save();

Action fireAndForget = async () =>
{
    await Task.Delay(10);
    Console.WriteLine("background work finished");
};

fireAndForget();
Console.WriteLine("main continues");
await Task.Delay(50);

The Func<Task> action completes before the next line runs. The Action version starts the work and returns at the first await, so the main code continues first.

Action fireAndForget = async () =>
{
    await Task.Delay(10);
    Console.WriteLine("background work finished");
};

The compiler allows the conversion because an async lambda whose body has no return value can be async void. This form is appropriate only for top-level event handlers, where something else owns the lifetime of the operation. For application code, prefer Func<Task> or a method that returns Task and await it.

$ dotnet run
saved
main continues
background work finished

Action, Func, and events

The three predefined delegate families have different jobs. Action represents work with no result, Func represents work that produces a value, and Predicate represents a yes-or-no test. A method that takes Action usually performs the work itself and reports through the callback; a method that takes Func asks the callback for a value.

An event is a different construct, even though it is built on a delegate. An Action is usually passed explicitly to one method, while an event is a member that many subscribers can attach to and that only the publisher can raise. If the callback should be optional, a nullable Action? parameter with ?. invocation is enough; if subscribers should be able to come and go, use an event.

The Func, Predicate, delegate, and events tutorials cover the related types in more detail.

Source

Action delegate

Action<T> delegate

Delegates

In this article we have used the Action and Action<T> delegates in C#. See also the Func, Predicate, lambda expression, and events tutorials.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all C# tutorials.