Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

C# Action Delegate

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Action Delegate With An Anonymous Method

As seen in the following example, you can also use an Action delegate with an anonymous method.
Example:

Action<string> val = delegate(string str)
{
    Console.WriteLine(str);
};
val("This is an Example");

Action Delegate With the Lambda Expressions 

You may also utilize an Action delegate with lambda expressions, as seen in the following example:

Example:

Action<string> val = str = > Console.WriteLine(str);
val("This is an Example");

Advantages of Action Delegates

  1. The delegate definition is simple and rapid.
  2. This shortens the code.
  3. Throughout the program, a compatible type is used.

Frequently Asked Questions

What is the use of Action Delegate in C#? 

The Action delegate is often used for methods that do not have a return value.

How do you use an action delegate? 

You can send a method as a parameter to the Action delegate without explicitly specifying a custom delegate.

What is the difference between Func and Action delegate? 

The difference between an Action and a Func delegate is that the Action delegate does not return a value.

Can action delegate be used with an anonymous function? 

Yes action delegate be used with an anonymous function

Can action delegate be used with lambda expression? 

Yes, action delegate is used with a lambda expression.

Conclusion

In this article, we had a look at Action Delegate in C# and how we can use it with anonymous function and lambda expressions. We hope that this blog has helped you enhance your knowledge regarding Action Delegates and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Explore more:

Topics covered
1.
Introduction
2.
Syntax:
3.
Action Delegate With An Anonymous Method
4.
Action Delegate With the Lambda Expressions 
5.
Advantages of Action Delegates
6.
Frequently Asked Questions
6.1.
What is the use of Action Delegate in C#? 
6.2.
How do you use an action delegate? 
6.3.
What is the difference between Func and Action delegate? 
6.4.
Can action delegate be used with an anonymous function? 
6.5.
Can action delegate be used with lambda expression? 
7.
Conclusion