Action Delegate in C#

The Action delegate in C# is a built-in generic delegate type that represents a method with no return value (void). It can accept zero or more input parameters and is particularly useful for passing methods as parameters or storing method references.

Syntax

Following is the basic syntax for declaring an Action delegate −

Action actionDelegate = MethodName;

For Action delegates with parameters −

Action<T> actionDelegate = MethodName;
Action<T1, T2> actionDelegate = MethodName;
Action<T1, T2, T3> actionDelegate = MethodName;

Where T, T1, T2, etc. are the parameter types.

Using Action Delegate with Single Parameter

Example

using System;

public class Demo {
    public static void Main() {
        Action<int> del = Display;
        del(2);
    }

    public static void Display(int val) {
        Console.WriteLine("Value: " + val);
    }
}

The output of the above code is −

Value: 2

Using Action Delegate with Multiple Parameters

Example

using System;

public class Demo {
    public static void Main() {
        Action<string, int> del = ShowDetails;
        del("John", 25);
        
        Action<int, int, int> mathAction = AddNumbers;
        mathAction(10, 20, 30);
    }

    public static void ShowDetails(string name, int age) {
        Console.WriteLine("Name: " + name + ", Age: " + age);
    }
    
    public static void AddNumbers(int a, int b, int c) {
        Console.WriteLine("Sum: " + (a + b + c));
    }
}

The output of the above code is −

Name: John, Age: 25
Sum: 60

Using Action with Lambda Expressions

Action delegates work seamlessly with lambda expressions and anonymous methods −

Example

using System;

public class Demo {
    public static void Main() {
        Action<string> greet = (name) => Console.WriteLine("Hello, " + name);
        greet("Alice");
        
        Action<int, int> calculate = (x, y) => {
            int result = x * y;
            Console.WriteLine("Product: " + result);
        };
        calculate(5, 8);
    }
}

The output of the above code is −

Hello, Alice
Product: 40

Action Delegate Types

Action Type Parameters Usage
Action None Methods with no parameters and void return
Action<T> One parameter of type T Methods with one parameter and void return
Action<T1,T2> Two parameters of types T1, T2 Methods with two parameters and void return
Action<T1,T2,...T16> Up to 16 parameters Methods with multiple parameters and void return

Conclusion

Action delegates in C# provide a convenient way to store and invoke methods that return void. They support up to 16 parameters and work well with lambda expressions, making them ideal for event handling, callback methods, and functional programming scenarios.

Updated on: 2026-03-17T07:04:35+05:30

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements