Func, Action And Local Function in C#


This article intends to demonstrate two Func and Action generic delegates and Local function in C# 7.0. The Func and Action are already available in C# while Local function is introduced in C# 7.0.

These three methods are used to in another method internally as per requirements. The below table demonstrates about each method.

# Func Action Local Function
1 It is a generic delegate. It is a generic delegate. It is method which defined in another method body.
2 It has 0 to 16 parameters. It has 0 to 16 parameters. It has 0 to 32 parameters but it’s code smell with too many parameters.
3 It always returns a value. It doesn’t return value. It returns value that could be void.
4 It must be defined before its call. It must be defined before its call. It could be defined later in the method.

Table 1: Summary of methods

You may like to read

  1. Ref Return and Ref Local In C#
  2. Pattern Matching In C#
  3. Expression Bodied Members In C#
  4. Tuple In C# 7.0

Working with Func


A method has so many lines, then we do refactor it based on functionalities. When a method refactors then we pass parameters in method and define local variable as per requirement. In Func, we create it based on functionality in another method. We pass parameters and it returns a value. It can also use outer method variables.

Let’s create an example. This program checks whether a number is a prime number or not. This example uses Func with a lambda expression to check prime number. The following code snippet for same.

As per the preceding example, It uses Func which has two parameters, the first parameter is an input parameter which takes an int value, while another is out parameter which uses to returns a bool value.

Here, an input parameter named p goes to an anonymous function Func, which returns true, if the input is a prime number.

Now, run the application. It shows the result as per the image given below.

Output 1: Func Result

Working with Action


The Action is a framework-provided delegate type. It uses as closure or callback. As it is defined in another method so it is a super private method. It has input parameters only and doesn’t return any value that’s why it is most commonly used for things like List.

Let’s create an example. This prints a Fibonacci series. It uses Action with a lambda expression to print a Fibonacci series. The following code snippet for the same.

As per the preceding example, Action has one parameter named f which uses as an input parameter.it defines length of Fibonacci series.

Now, run the Application. It shows the result, as shown in the image given below.

Output 2: Action Result

Working with Local Function


The Local function is introduced in C# 7.0. It defines in the body of any method and property’s getter and setter. All the parameters and the local variables in the outer function are in scope of the Local function. Local functions can use ref and out parameters.

Let’s create an example. This calculates a factorial number of a given number. It uses a local function named GetFactorial to calculate factorial of the given number. The following code snippet for same.

As per the preceding example, It uses a local function named GetFactorial in the Main method. It has a parameter and returns a long type value.

Now, run the Application. It shows the result, as shown in the image given below.

Output 3: Local Function in C# result

Conclusion


These two predefined generic delegates and local function use as a nested method.These reduce code but don’t make readable.These generic delegates use in the LINQ.