Open In App

C# Action Delegate

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Action delegate in C# is a built-in generic delegate type provided by the .NET framework. It is defined under the System namespace and is used to represent methods that do not return a value i.e. methods with a void return type. The Action delegate can accept from 0 to 16 input parameters of any type. It is commonly used to encapsulate methods that perform actions without returning a result.

Example 1: This example is the simplest example to demonstrate the use of the Action delegate.

C#
// Action delegate with no parameters 
using System;

class Geeks
{
    // Method that matches the Action delegate signature
    public static void PrintMessage()
    {
        Console.WriteLine("Hello, Geek");
    }

    static void Main()
    {
        // Using Action delegate with no parameters
        Action action = PrintMessage;
        action();  
    }
}

Output
Hello, Geek

Explanation: In this example, we use an Action delegate with no parameters. The PrintMessage method is assigned to the delegate and when the delegate is invoked, it prints “Hello, Geek”.

Syntax

Action with one parameter:

public delegate void Action<in P>(P obj);

Action with two parameters:

public delegate void Action<in P1, in P2>(P1 arg1, P2 arg2);

Action with no parameters:

public delegate void Action();

Where P, P1, and P2 are the types of the input parameters.

Important Points:

The only difference between Action Delegates and Function Delegates is that Action Delegates does not return anything i.e. having void return type.

An Action Delegate can also be initialized using the new keyword.

Action<int> val = new Action<int>(myfun);


An Action Delegate can also be initialized by directly assigning to a method.

Action<int> val = myfun;

Example 2: Using Action Delegate with Two Parameters

C#
// Action delegate with two parameters 
// to subtract two numbers
using System;

class Geeks
{
    // Method that matches the Action delegate signature
    public static void SubtractNumbers(int p, int q)
    {
        Console.WriteLine(p - q); 
    }

    static void Main()
    {
        // Using Action delegate with two parameters
        Action<int, int> action = SubtractNumbers; 
        action(20, 5); 
    }
}

Output
15

Explanation: In this example, the Action delegate is used to pass two parameters (int), and the SubtractNumbers method performs the subtraction and outputs the result.

Example 3: Using Anonymous Methods and Lambda Expressions with Action

Anonymous Method Example:

// Anonymous method using Action delegate to print a string

Action<string> action = delegate(string str)

{

Console.WriteLine(str); // Output the string

};

action(“GeeksforGeeks”); // Output: GeeksforGeeks

Lambda Expression Example:

// Lambda expression using Action delegate to print a string

Action<string> action = str => Console.WriteLine(str); // Output the string

action(“GeeksforGeeks”); // Output: GeeksforGeeks

Both examples achieve the same result, but lambda expressions provide a more concise and readable way to define the method logic.



Next Article
Article Tags :

Similar Reads