Difference between Func delegate and Action delegate in C#

In this article, we will discuss the difference between Func delegate and Action delegate in C#. Before understanding the differences, let us understand each term. Methods defined using specific signatures are referred to by a type called delegate in C#.

What is a Delegate?

A delegate is a value type that makes it possible to invoke another method with a similar signature when the method is not known before deployment. The "delegate" keyword is applied to create delegates that are functions serving as callback methods. The delegate can either be built-in or nested within a class. The major difference is that the former is applied to those delegates that return value while the latter is used for delegates that have no return value. Programs can make use of delegates to implement callback methods, handle events, and accomplish tasks at which it is necessary to execute a method at a later time. C# supports delegates in various forms, and Action and Func are the two most popular ones that are found in the System namespace.

What is Action Delegate in C#?

Action Delegate is a formal system delegate, which is a generic type of delegate. In turn, it correctly defines a generic delegate instead of defining a custom delegate, thereby increasing the code readability and optimization. It is a part of the System namespace. It can also have 1 param for minimum and, at most, 16 params for maximum input parameters. The most common purpose of the Action delegate is to pass it to methods where the return value is not required. In simple words, Action delegate is used with those methods where the return type is void. It might also include the same type of parameter as well as different types of parameters.

Syntax:

It has the following syntax:

Here, Param1 and Param2 are constraints of the input parameters, and arg1 and arg2 are formal parameters of the method, which is capsulated by an Action delegate. The Action delegate is instead a predefined delegate type that supports methods with zero or more arguments and no output value as a return.

Example:

Let us take an example to illustrate the action delegate in C#.

Output:

 
 23  

Explanation:

In the above program, we encapsulate the action delegate, which will improve the readability of the program and reduce the code size. Here, the 'action' delegate has two arguments, 'error' and 'completion'. The Action delegate will eventually call the paramFun function through the method we have directly assigned to it.

What is the Func Delegate in C#?

Func is a special delegate from the System namespace commonly used in C#. It serves as a versatile tool for defining methods that either take one input parameter or, in cases where there are no parameters, return a value. By utilizing Func, there is no need to declare a custom delegate, simplifying the project and enhancing efficiency.

Named aptly, Func is tailored for methods that produce a return value, making it a suitable delegate to represent such methods. This functionality streamlines the coding process and promotes cleaner, more concise code.As Func is a delegate with generic capabilities; therefore, it belongs to the system namespace. Those can be a minimum of 0, at most 16 parameters in it, and it can display only 1 output parameter. The last parameter of the Func delegate is the output type, which is considered as return type and relates to the result. It may also contain the same type or the different type parameters.

Syntax:

It has the following syntax.

Example:

Let us take an example to illustrate the Func delegate in C#.

Output:

 
853632  

Explanation:

Inside the FuncDelegate, a delegate is declared using the delegate keyword, named fun_delegate. This delegate spec has a method call signature that takes four integer arguments and returns an integer. Subsequently, method delegatemethod is declared by a static type. Such a technique compares the definition of fun_delegate with its statically appended signature. It combines the four input numbers s1, d1, f1, and g1 and gives back the result.

In the Main() method, an object obj classified from fun_delegate protocol gets initialized and passed the method called delegatemethod. It translates into obj either invoking the delegate method directly or invoking the delegatemethod method.

Finally, obj delegate is invoked in which its four integer arguments are 19, 39, 32, and 36, which is represented as obj(19, 39, 32, 36). The output is shown on the console by calling Console.WriteLine() method.

Key difference between Func delegate and Action delegate

Difference between Func delegate and Action delegate in C#

Here are the differences between Func delegate and Action delegate in C#:

FunctionFuncAction
Return typeIt has the return type.No return type.
Number of parametersIt can have multiple input parameters.Mostly for methods with input parameters.
Number of type parametersThe return type can have one or more, but the input parameter can have more than one.At least one for each measuring parameter.
UsageIt is for ways having values return.Acceptable means of return value.
Delegate signatureFuncAction
ExampleFuncAction
Error handlingUse methods for encapsulation.Use methods for encapsulation and side effects

Conclusion:

In conclusion, both Func delegates and Action delegates in C# have that feature in common where they can package specific method signatures into a single entity. The method to be picked is only determined by the type of operation (whether it returns a value or not), which is subsequently applied for various reasons. Func is a function form that is employed when a return value is expected, hence giving way to more encompassing possibilities for handling different types of data. Moreover, Action is used in the rest of the cases where the function performed is of a non-returning type and provides a clear fit for situations calling for side effects or those that return a void. Thus, by understanding their differences, developers can pick the corresponding delegate for their various cases, promoting cleaner and more readable programming.






Latest Courses