C# | Action Delegate

Action delegate is an in-built generic type delegate. This delegate saves you from defining a custom delegate as shown in the below examples and make your program more readable and optimized. It is defined under System namespace. It can contain minimum 1 and maximum of 16 input parameters and contain only one output parameter. The Action delegate is generally used for those methods which do not contain any return value, or in other words, Action delegate is used with those methods whose return type is void. It can also contain parameters of the same type or of different types.

Syntax:

// One input parameter
public delegate void Action < in P > (P obj);

// Two input parameters
public delegate void Action < in P1, in P2 >(P1 arg1, P2 arg2);

Here, P, P1, and P2 are the type of the input parameters & arg1 and agr2 are the parameters of the method that Action delegate encapsulates.

Example: Below program illustrate how we create a custom delegate.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate delegates
using System;
  
class GFG {
  
    // Declaring the delegate
    public delegate void my_delegate(int p, int q);
  
    // Method
    public static void myfun(int p, int q)
    {
        Console.WriteLine(p - q);
    }
  
    // Main method
    static public void Main()
    {
  
        // Creating object of my_delegate
        my_delegate obj = myfun;
        obj(10, 5);
    }
}

chevron_right


Output:

5

Example: It demonstrates the use of Action delegate.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate Action delegates
using System;
  
class GFG {
  
    // Method
    public static void myfun(int p, int q)
    {
        Console.WriteLine(p - q);
    }
  
    // Main method
    static public void Main()
    {
  
        // Using Action delegate
        // Here, Action delegate 
        // contains two input parameters
        Action<int, int> val = myfun;
        val(20, 5);
    }
}

chevron_right


Output:

15

Explanation: In the above example, using Action delegate reduce the size of the code and make the program more readable. Here action delegate contains two input parameters. And we directly assign the myfun method to the Action delegate.

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;
  • You can also use an Action delegate with an anonymous method as shown in the below example:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    Action<string> val = delegate(string str)
    {
        Console.WriteLine(str);
    };
    val("GeeksforGeeks");

    chevron_right

    
    

  • You can also use a Action delegate with the lambda expressions as shown in the below example:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    Action<string> val = str = > Console.WriteLine(str);
    val("GeeksforGeeks");

    chevron_right

    
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.