Delegates are used to create references to different methods in C#. Creating these references aims to make the methods accessible in an event-driven approach. There are two major predefined delegates that we can utilize, the Function and the Action delegate. 

In this post, I will cover the purpose and the use of these two types of predefined delegates in C#.

What is Function Delegate <Func> in C#?

The function delegates are used to create references to the methods that return some value to the caller. The syntax for creating a Func delegate is:

Func <param1,param2,...,paran16,returnType> objVar = methodName;

In this syntax:

  • Func is the keyword used to represent a function delegate
  •  <param1, …, param16> represents the arguments of the method referenced by the delegate. However, the last argument returnType represents the return value of the method
  • objVar is the variable that will be used to refer to the method
  • methodName specifies the method to be referenced by the delegate

Note: The limit of parameters is 17. The First 16 represent the method’s inputs, and the last one represents the return value type.

Example of Func Delegate

To demonstrate the use of the Func delegate, we are going to create a method that takes three inputs: int, float, and double, and returns the sum of the three inputs as a double:

public static double addThree(int x, float y, double z){
     return x+y+z;
}

After that, create a Func delegate in the main method and invoke it by using the Invoke() method:

static void Main(){
            Func<int, float, double, double> obj1= addThree;
            double result = obj1.Invoke(56,56.23f,193.112);
            Console.Write(result);
        }

Execute the program, and we will have the following output displayed on the terminal:

We have the sum of the three inputs displayed on the terminal, meaning that the Func delegate is working perfectly.

What is Action Delegate <Action> in C#?

An Action Delegate is used to create a reference to a method that performs an action but doesn’t return any value to the caller. In simpler terms, the delegates to the void methods are Action Delegates. The syntax of the Action delegate is:

Action <param1,param2,...paran16> objVar = methodName;

In this syntax:

  • Action is the keyword used to represent a function delegate
  •  <param1, …, param16> represents the arguments of the method referenced by the delegate. Unlike the Func delegate, no return value parameter is required in the Action delegate.
  • objVar is the variable that will be used to refer to the method
  • methodName specifies the method to be referenced by the delegate

Note: The limit of parameters is 16

Example of Action Delegate

To demonstrate the use of the Action delegate, we are going to create a method that will take three numbers as input and print their sum onto the console:

public static void printSum(int x, float y, double z){
            Console.Write(x+y+z);
      }

When this is done, let’s create an Action delegate and invoke this method using the Invoke() method:

static void Main(){
            Action<int, float, double> obj1 = printSum;
            obj1.Invoke(23,47.3f,595.11);
        }

When this code is executed, it will give us the following output on the console:

By using the action delegate, we executed a void method successfully.

Summary

The Action and the Function are predefined delegate templates that refer to specific methods. The Func delegate represents a method that returns some value to the caller. At the same time, the Action delegate refers to the void method. Using these two delegate templates instead of the vanilla delegates saves time and lines of code.

Categorized in: