Action delegate in C# is a built-in generic delegate type provided by the .NET framework. It is defined under the System namespace and is used to represent methods that do not return a value i.e. methods with a void return type. The Action delegate can accept from 0 to 16 input parameters of any type. It is commonly used to encapsulate methods that perform actions without returning a result.
Example 1: This example is the simplest example to demonstrate the use of the Action delegate.
C#
// Action delegate with no parameters
using System;
class Geeks
{
// Method that matches the Action delegate signature
public static void PrintMessage()
{
Console.WriteLine("Hello, Geek");
}
static void Main()
{
// Using Action delegate with no parameters
Action action = PrintMessage;
action();
}
}
Explanation: In this example, we use an Action delegate with no parameters. The PrintMessage method is assigned to the delegate and when the delegate is invoked, it prints “Hello, Geek”.
Syntax
Action with one parameter:
public delegate void Action<in P>(P obj);
Action with two parameters:
public delegate void Action<in P1, in P2>(P1 arg1, P2 arg2);
Action with no parameters:
public delegate void Action();
Where P, P1, and P2 are the types of the input parameters.
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;
Example 2: Using Action Delegate with Two Parameters
C#
// Action delegate with two parameters
// to subtract two numbers
using System;
class Geeks
{
// Method that matches the Action delegate signature
public static void SubtractNumbers(int p, int q)
{
Console.WriteLine(p - q);
}
static void Main()
{
// Using Action delegate with two parameters
Action<int, int> action = SubtractNumbers;
action(20, 5);
}
}
Explanation: In this example, the Action delegate is used to pass two parameters (int), and the SubtractNumbers method performs the subtraction and outputs the result.
Example 3: Using Anonymous Methods and Lambda Expressions with Action
Anonymous Method Example:
// Anonymous method using Action delegate to print a string
Action<string> action = delegate(string str)
{
Console.WriteLine(str); // Output the string
};
action(“GeeksforGeeks”); // Output: GeeksforGeeks
Lambda Expression Example:
// Lambda expression using Action delegate to print a string
Action<string> action = str => Console.WriteLine(str); // Output the string
action(“GeeksforGeeks”); // Output: GeeksforGeeks
Both examples achieve the same result, but lambda expressions provide a more concise and readable way to define the method logic.
Similar Reads
C# Delegates
A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application),
6 min read
C# Func Delegate
In C#, a delegate is a type that references a method. When creating a custom delegate, we follow these steps: Declare a delegate with a signature matching the target method.Create an instance of the delegate.Invoke the method using the delegate.But defining custom delegates manually can be repetitiv
3 min read
C# Predicate Delegate
A Predicate delegate is an in-built generic type delegate. This delegate is defined under System namespace. It works with those methods which contain some set of criteria and determine whether the passed parameter fulfill the given criteria or not. This delegate takes only one input and returns the
2 min read
Multicast Delegates in C#
A type-safe function pointer is a delegate. It means that the delegate contains a reference to a method or function, and that when we invoke the delegate, the method to which it refers will be executed. The delegate signature and the method to which it points must both be signed. The method must be
3 min read
Extension Method in C#
In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type. It is
5 min read
C# Thread Class
In C#, multi-threading is implemented using the Thread class, which is part of the System.Threading namespace. This class allows for the creation, management, and control of threads within an application. The Thread class provides various methods and properties to handle thread execution, prioritize
7 min read
Delegates vs Interfaces in C#
A Delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered. Example: C/C+
3 min read
Anonymous Method in C#
An anonymous method is a method which doesnât contain any name which is introduced in C# 2.0. It is useful when the user wants to create an inline method and also wants to pass parameter in the anonymous method like other methods. An Anonymous method is defined using the delegate keyword and the use
3 min read
ASP.NET MVC Life Cycle
The goal of this article is to provide a good understanding of the MVC pipeline. The life cycle is basically is set of certain stages which occur at a certain time. MVC actually defined in two life cycles, the application life cycle, and the request life cycle. The application life cycle, in which t
8 min read
C# Main Thread
In C#, threads are the smallest units of execution that allow parallel execution of code, enabling multiple tasks to run concurrently within a single process. The Thread class in the System.Threading namespace is used to create and manage threads. In C#, the Main method is the entry point of any con
5 min read