English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Action Delegate

Action is a delegate type defined in the System namespace. The Action type delegate is the same as the Func delegate, except that the Action delegate does not return a value. In other words, the Action delegate can be used with methods that have a void return type.

What is the Action generic delegate

Action<T> is a built-in generic delegate in .NET Framework, which can be used to pass methods as parameters with the Action<T> delegate without explicitly declaring a custom delegate. The enclosed method must correspond to the method signature defined by the delegate. That is, the enclosed method must have a parameter passed to it by value and cannot have a return value.

For example, the following delegate prints an int value.

public delegate void Print(int val);
static void ConsolePrint(int i)
{
    Console.WriteLine(i);
}
static void Main(string[] args)
{           
    Print prnt = ConsolePrint;
    prnt(10);
}
Output:
10

You can use the Action delegate instead of the defined Print delegate, for example:

static void ConsolePrint(int i)
{
    Console.WriteLine(i);
}
static void Main(string[] args)
{
    Action<int> printActionDel = ConsolePrint;
    printActionDel(10);
}

You can use the new keyword or directly assign a method to initialize the Action delegate:

Action<int> printActionDel = ConsolePrint;
//or
Action<int> printActionDel = new Action<int>(ConsolePrint);

The Action delegate can accept at most16different types of input parameters.

Action delegate and anonymous method

You can also assign an anonymous method to an Action delegate, for example:

static void Main(string[] args)
{
    Action<int> printActionDel = delegate(int i)
                                {
                                    Console.WriteLine(i);
                                };
    printActionDel(10);
}
Output:
10

Action delegate and Lambda expression

Lambda expressions can also be used with Action delegates:

static void Main(string[] args)
{
    Action<int> printActionDel = i => Console.WriteLine(i);
       
    printActionDel(10);
}

Therefore, you can use any method that does not return an Action delegate type.

Example of using the Action delegate

  • The Action delegate can accept at least 0 and at most16parameters, and no return value.

  • The Action represents a delegate with no parameters and no return value.

  • The Action<int, string> represents a delegate with input parameters int, string and no return value.

  • The Action<int, string, bool> represents a delegate with input parameters int, string, bool and no return value.

  • Action<int, int, int, int> indicates that there are four input parameters.4an int parameter, a delegate with no return value.

Advantages of Action and Func delegates

  1. Define delegates easily and quickly.

  2. Make the code concise.

  3. compatible types throughout the entire application.

Key Points to Remember

  1. Action delegates are similar to Func delegates, but Action delegates do not return any content. The return type must be void.

  2. Action delegates can have from 0 to16an input parameter.

  3. Action delegates can be used with anonymous methods or lambda expressions.