Action Delegates in .NET Core C#: Syntax, Usage and Examples

In the previous article, we learned about delegates and function delegates. If you haven't checked, then first, please refer to the below articles.

Introduction

One built-in generic type of delegate is the action delegate. This delegate makes your program more readable and efficient by eliminating the need for you to define a custom delegate, as demonstrated in the examples below. Under the System namespace, it is defined. It has no output parameters and a minimum of 1 and a maximum of 16 input parameters. The Action delegate is typically used with methods that have a void return type, or methods that don't contain any kind of value at all. Additionally, it may contain parameters of one type or more than one type.

Syntax

// One input parameter
public delegate void Action<in param1>(param1 obj);

// Two input parameters
public delegate void Action<in param1, in param2>(param1 arg1, param2 arg2);

As an illustration for custom delegates.

using System;

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

class Program
{
    static void Adds(int a, int b)
    {
        Console.WriteLine("Addition: {0}", a + b);
    }

    static void Main()
    {
        Sum addition = Adds;
        addition(22, 45);

        Console.ReadLine();
    }
}

Custom Delegates

As an illustration of an action delegate.

using System;

class Program
{
    static void Main()
    {
        Action<int, int> addition = Add;
        addition(22, 45);

        Console.ReadLine();
    }

    static void Add(int a, int b)
    {
        Console.WriteLine("Addition: {0}", a + b);
    }
}

Action delegate

Using Action Delegate in the aforementioned example results in smaller code and easier-to-read software. Two input parameters are contained in this action delegate. Additionally, we give the Action delegate direct access to the Add method.

An Action delegate can be initialized by assigning a method directly or by using the new keyword.

Syntax

using System;

class Program
{
    static void Main()
    {
        // Example 1: Action delegate assignment
        Action<int> objectname = method;

        // Example 2: Action delegate assignment with explicit instantiation
        Action<int> objectname = new Action<int>(method);
    }

    static void method(int param)
    {
        // Method implementation
    }
}

As an illustration,

using System;

class Program
{
    static void Main()
    {
        // Example 1: Action delegate assignment
        Action<int, int> addition = Add;
        addition(22, 45);

        // Example 2: Action delegate assignment with explicit instantiation
        var add = new Action<int, int>(Add);
        add(34, 65);

        Console.ReadLine();
    }

    static void Add(int a, int b)
    {
        Console.WriteLine("Addition: {0}", a + b);
    }
}

Illustration

An Action delegate may also be assigned an Anonymous method, as in the following instances.

using System;

class Program
{
    static void Main()
    {
        Action<int, int> addition = delegate (int a, int b)
        {
            Console.WriteLine("Addition: {0}", a + b);
        };
        
        addition(22, 45);
    }
}

Anonymous method

An Action delegate can also be utilized with a lambda expression.

using System;

class Program
{
    static void Main()
    {
        Action<int, int> addition = (a, b) => Console.WriteLine("Addition: {0}", a + b);
        addition(22, 45);
    }
}

Lambda expression

As such, you can use Action delegate types with any method that doesn't return a value.

Advantages of an Action Delegate

  1. Delegators are simple and quick to define.
  2. Shortens the code.
  3. Sorting that is compatible across the board.

Crucial Points to remember

  • Action delegate functions similarly to func delegate, but it doesn't return anything. The return type has to be null.
  • Action delegate input parameters range from 0 to 16.
  • Lambda expressions and anonymous methods can both be utilized with Action Delegate.

We learned the new technique and evolved together.

Happy coding!