Updated October 9, 2023
Introduction to C# Action Delegate
The built-in delegate belongs to the generic type defined under the System namespace. You can use it in methods that do not have any return value, which means they are called action delegates. An action delegate can contain a minimum of one input parameter and a maximum of sixteen input parameters, and these parameters can have the same data type or different data types. Using an action delegate in your program makes it 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);
input_paramter_type, input_paramter_type1, and input_paramter_type2 represent the types of input parameters, while argument1 and argument2 are the parameters used in the method encapsulated by the action delegate.
Working of Action Delegate in C#
- When you require an action delegate for use in methods that do not contain any return value, i.e., methods with a void return type, you refer to it as an action delegate.
- The action delegate belongs to a generic type and is defined within the 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 an action delegate is sixteen, and the type of parameters used can be of the same data type or different data types.
- By making use of action delegates in the program, the program becomes more optimized and readable.
Examples
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#");
}
}
Output:
The above program defines a class called “check” and invokes a method called “join,” which prints the parameter passed to it as the output on the screen. Next, the main method calls the “join” function by defining an action delegate. The program then defines an action delegate that 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 provided program, we define a class named “check.” This class includes a method named “power,” which calculates the power of two given parameters passed to it and displays the result on the screen. Afterward, we invoke the main method, wherein we call the “power” function by defining an action delegate. We also define an action delegate that accepts 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, we define a class called “check.” Within this class, we define a method named “power” that takes two parameters as inputs. This method calculates the power of the given number and displays the result on the screen. Following that, we invoke the main method, where we call the “power” function by defining an action delegate. Specifically, we define an action delegate that accepts two input parameters.
Conclusion
In this tutorial, we understand the concept of action delegate in C# through definition, the syntax of action delegate, and the 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 –