Introduction
An action delegate is a pre-existing generic type delegate. The System namespace defines the delegate type Action. The difference between an Action and a Func delegate is that the Action delegate does not return a value. The Action delegate is typically used with methods that have no return value, or methods having void return types. It may also include parameters of the same or other sorts.
Syntax:
public delegate void Action < in C > (C obj);
public delegate void Action < in C1, in C2 >(C1 arg1, C2 arg2);
Here, C, C1, and C2 are the types of input arguments, while arg1 and agr2 are the method parameters encapsulated by the Action delegate.
The new keyword can also be used to initialize an Action Delegate.
Action<int> val = new Action<int>(my_fun);
Action<int> val = my_fun;
Example
class ActionDelegate {
public delegate void delegate(int p, int q);
public static void fun(int p, int q)
{
Console.WriteLine(p - q);
}
static public void Main()
{
delegate obj = fun;
obj(25, 5);
}
}
Output:
20