C# Action Delegate

In c#, Action is a built-in generic delegate same like Func delegate to hold the reference of one or more methods but the only difference is the Action delegate will not return any value.

 

In previous chapters, we learned about delegates and that will be used like as shown following to hold the reference of methods which is having the same signature.

 

using System;

 

namespace TutlaneExamples

{

    // Declare Delegate

    public delegate void SampleDelegate(int a, int b);

    class Program

    {

        static void Main(string[] args)

        {

            SampleDelegate dlgt = Add;

            dlgt(10, 90);

            dlgt = Subtract;

            dlgt(10, 90);

            Console.ReadLine();

        }

        public static void Add(int a, int b)

        {

            Console.WriteLine("Add Result: {0}", a + b);

        }

        public static void Subtract(int x, int y)

        {

            Console.WriteLine("Subtract Result: {0}", x - y);

        }

    }

}

If you observe the above example, we created a delegate object called “SampleDelegate” to hold the reference of Add & Subtract methods.

 

When we execute the above example, we will get the result like as shown below.

 

Add Result: 100

Subtract Result: -80

To avoid the declaration of custom delegate object (SampleDelegate) like as we defined in the above example, the generic built-in delegates such as Func, Action, Predicate has been introduced in C# 3.0.

 

From C# 3.0 onwards the Action delegate will available automatically with System namespace and it will accept zero or more (16) input parameters and does not return a value.

C# Action Delegate Syntax

Following is the syntax of declaring an Action delegate with one input parameter in c#.

 

public delegate void Action<in T>(T arg);

Here, the parameters in the angle bracket < > will be considered as input parameters and void is the return type.

 

In case, if we want to create an Action delegate with two input parameters that would be like as shown below.

 

public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);

Like this, an Action delegate can include 0 to 16 input parameters of different types based on our requirements. 

C# Action Delegate Example

Following is the example of defining the Action delegate to hold the reference of one or more methods which is having same method signature.

 

using System;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            Action<int, int> dlgt = Add;

            dlgt(10, 90);

            dlgt = Subtract;

            dlgt(10, 90);

            Console.ReadLine();

        }

        public static void Add(int a, int b)

        {

            Console.WriteLine("Add Result: {0}", a + b);

        }

        public static void Subtract(int x, int y)

        {

            Console.WriteLine("Subtract Result: {0}", x - y);

        }

    }

}

If you observe the above example, we created an Action delegate object (dlgt) with two input parameters (int) and assigned the methods directly to the delegate object.

 

Here, the declaration of Action<int, int> dlgt is same as SampleDelegate object in the previous example.

 

When we execute the above example, we will get the result like as shown below.

 

Add Result: 100

Subtract Result: -80

Every time while creating the Action delegate we must need to remember that we can include 0 to 16 input parameters of different types and that is optional.

 

In c#, we can also initialize an Action delegate using new keyword with required input parameters like as shown below.

 

Action<int, int> dlgt = new Action<int,int>(Add);

C# Action Delegate with Anonymous Method

In c#, we can assign the anonymous method directly to the Action delegate by using delegate keyword like as shown below.

 

static void Main(string[] args)

{

    Action<int, int> dlgt = delegate(int x, int y)

    {

        Console.WriteLine("Result: {0}", x + y);

    };

    dlgt(10, 90); // Result: 100

}

If you observe the above code, we assigned an anonymous method directly to Action delegate object (dlgt) using delegate keyword.

C# Acton Delegate with Lambda Expressions

In c#, we can also use Action delegate with lambda expressions. The lambda expressions are the shorthand way for declaring the anonymous method.

 

static void Main(string[] args)

{

    Action<int, int> dlgt = (x, y) =>

    {

        Console.WriteLine("Result: {0}", x + y);

    };

    dlgt(10, 90); // Result: 100

}

C# Action Delegate Overview

Following are the important points which we need to remember about Action delegate in c#.

 

  • In c#, Action is a built-in generic delegate same like Func delegate but the only difference is the Action delegate will not return any value.
  • While creating an Action delegate, we can include 0 to 16 input parameters of different types.
  • We can use Action delegate in anonymous methods and lambda expressions.
 
 
 

Topics Covered