C# Program to Illustrate Actions

What are Actions in C#?

Actions in C# are delegate types representing methods with no return value. They’re used for callbacks and event handling, making code more flexible and maintainable.

Declaration and Usage of Actions:

Here’s the syntax for declaring and using Actions in C#:

Declaration:

Action<parameter1Type, parameter2Type, ...> actionName;

To declare an Action, use the Action keyword followed by the method’s parameter types. For instance, Action<int, string> represents a method with an int and string parameter and no return value.

Usage:

advertisement
actionName = methodName;
actionName(parameter1Value, parameter2Value, ...);

To use the Action, create an instance and assign a method to it. Then, call the Action with the specified arguments to execute the method.

Action Usage Example
/*
 * C# Program to Illustrate Actions
 */
 
using System;
 
class Program
{
    static void Main()
    { 
        Action<int> action1 =(int x) => Console.WriteLine("OUTPUT {0}", x);
        Action<int, int> action2 =(x, y) => Console.WriteLine("OUTPUT {0} and {1}", 
                                                                x, y);
        action1.Invoke(1111);
        action2.Invoke(200, 3000);
        Console.Read();
    }
}
Program Explanation

1. This C# program illustrates the use of actions.
2. We create two objects ‘action1‘ and ‘action2‘ using Action<int>.
3. Action<int> encapsulates a method with a single parameter and no return value.
4. Action objects return no values. Pass ‘action1‘ and ‘action2‘ variables as arguments to Invoke().

👉 Apply Now for Free C Certification - December 2025
Program Output:
OUTPUT 1111
OUTPUT 200 and 3000
Multicast Actions:

Multicast Actions in C# are delegates that can reference multiple methods at once. They enable calling multiple methods simultaneously using a single delegate instance, achieved by using the “+” operator to add methods and the “-” operator to remove them.

advertisement
Action actionName = new Action(method1);
actionName += new Action(method2);
actionName += new Action(method3); // To add a method from the list
actionName -= new Action(method2); // To remove a method from the list

In the example, actionName is created using new Action, referring to multiple methods (method1, method2, method3, etc.). += adds methods, and -= removes them. When actionName called, all referenced methods execute in sequence.

Benefits of Actions:
  • Actions in C# offer benefits such as code reusability by using the same Action with different methods, and decoupling to make the code more maintainable.
  • Common use cases include UI event handling, background tasks, and callbacks in asynchronous programming.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.