Introduction to C# Action Delegate
The built-in delegate which is of the generic type defined under the namespace System namespace and which can be used in those methods not containing any return value meaning the methods whose return type is void is called an action delegate and the minimum number of input parameters that can be contained in an action delegate is one and the maximum number of input parameters that can be contained in action delegate is sixteen and type of the parameters used can be of same data type or different data types and by making use of action delegate in the program, the program becomes more optimized and readable. In this topic, we are going to learn about C# Action Delegate.
The syntax for Action Delegate in C# is as follows:
public delegate void Action < in input_parameter_type > (input_parameter_type object);
public delegate void Action < in input_parameter_type1, in input_parameter_type2 >( input_parameter_type1 argument1, input_parameter_type2 argument2);
where input_paramter_type, input_paramter_type1, input_paramter_type2 are the type of the input parameters and argument1, argument2 are the parameters used in the method that is encapsulated by the action delegate.
Working of Action Delegate in C#
- Whenever there is a need to make use of action delegate which can be used in those methods not containing any return value meaning the methods whose return type is void is called an action delegate.
- The action delegate which is of generic type and is defined under the namespace System namespace.
- The minimum number of input parameters that can be contained in an action delegate is one and the maximum number of input parameters that can be contained in action delegate is sixteen and the type of the parameters used can be of the same data type or different data types.
- By making use of action delegate in the program, the program becomes more optimized and readable.
Examples of C# Action Delegate
Here are the following examples mention below :
Example #1
C# program to demonstrate Action Delegate to concatenate the given string and print the statement as the output on the screen.
Code:
using System;
//a class called check is defined
class check
{
// a method called join is called which takes the parameter passed to the method and prints it as the output on the screen
public static void join(string str)
{
Console.WriteLine("Welcome to {0}", str);
}
// main method is called within which the join function is called by defining an action delegate
static public void Main()
{
//an action delegate is defined which takes one input parameter which is passed to the join method
Action<string> stringvalue = join;
stringvalue("C#");
}
}

4.6 (13,258 ratings)
View Course
Output:
In the above program, a class called check is defined. Then a method called join is called which takes the parameter passed to the method and prints it as the output on the screen. Then the main method is called within which the join function is called by defining an action delegate. Then an action delegate is defined which takes one input parameter.
Example #2
C# program to demonstrate Action delegate to compute the power of a given number.
Code:
using System;
//a class called check is defined
class check
{
// a method called power is defined which takes two parameters passed to the method and calculates the power of the given number and displays it on the screen
public static void power(double number1, double number2)
{
Console.WriteLine("The power of the given number is {0}", Math.Pow(number1, number2));
}
// main method is called within which the power function is called by defining an action delegate
static public void Main()
{
//an action delegate is defined which takes two input parameters which is passed to the power method
Action<double, double> doublevalue = power;
doublevalue(2,2);
}
}
Output:
In the above program, a class called check is defined. Then a method called power is defined which takes two parameters passed to the method and calculates the power of the given number and displays it on the screen. Then the main method is called within which the power function is called by defining an action delegate. Then an action delegate is defined which takes two input parameters.
Example #3
C# program to demonstrate an action delegate to find the square of the given number.
Code:
using System;
//a class called check is defined
class check
{
// a method called power is defined which takes two parameters passed to the method and calculates the power of the given number and displays it on the screen
public static void square(int number)
{
Console.WriteLine("The square of the given number is {0}", number * number);
}
// main method is called within which the power function is called by defining an action delegate
static public void Main()
{
//an action delegate is defined which takes one input parameter which is passed to the square method
Action<int> answer = square;
answer(3);
}
}
Output:
In the above program, a class called check is defined. Then a method called power is defined which takes two parameters passed to the method and calculates the power of the given number and displays it on the screen. Then the main method is called within which the power function is called by defining an action delegate. Then an action delegate is defined which takes two input parameters.
Conclusion
In this tutorial, we understand the concept of action delegate in C# through definition, the syntax of action delegate, and working of action delegate in C# through programming examples and their outputs.
Recommended Articles
This is a guide to C# Action Delegate. Here we discuss the working of Action Delegate in C# and Examples along with the codes and outputs. You may also look at the following articles to learn more –