DEV Community

Cover image for Action and Func Delegates in C#
Alerechi Ordu
Alerechi Ordu

Posted on

Action and Func Delegates in C#

In this article, we will discuss the difference between Action and Func delegates in C#.

We’ll begin by explaining what exactly delegate is. Then, we’ll explain the meaning of Action and Func delegates. After that, we will delve into their use cases. Finally, we’ll round things up by summarizing the difference between Action and Func delegates.

Delegates

In C#, delegates provide a powerful mechanism for implementing callbacks and defining function pointers. Action and Func are two commonly used delegates in C#. In this article, we will explore the differences between Action and Func delegates, their syntax, and practical use cases.

Here is the link to the source code: https://github.com/Rechidesigns/Action-and-Func-delegates-in-C-sharp

Action Delegate

The Action delegate is a part of the System namespace, is a predefined generic delegate representing a method that takes parameters and does not return a value. It can handle various parameter counts from zero to sixteen.

We can consider an Action delegate as one that takes two integer parameters and prints their sum, below is an example of how we can do that.

public class IncrementDelegate
{
    public int RunIncrement(int incrementBy)
    {
        var number = 5;
        Action<int> increment = n => { number += n; };
        increment(incrementBy);
        return number;
    }
}
Enter fullscreen mode Exit fullscreen mode

Func Delegate

Similar to the Action delegate, the Func delegate is also part of the System namespace. However, unlike Action, the Func delegate represents a method that takes parameters and returns a value. The last type parameter in the Func delegate represents the return type.

For example, we can consider a Func delegate as one that takes two integer parameters and returns their sum

public class AdditionDelegate
{
    public int RunAddition(int x, int y)
    {
        Func<int, int, int> add = (a, b) => a + b;
        var addResult = add(x, y);
        return addResult;
    }
}
Enter fullscreen mode Exit fullscreen mode

We will be looking at their basic differences for more clarity

Key differences exist between the Action and Func delegates regarding their return type, the number of parameters they handle, and their usage.

Action does not return a value and can handle zero to sixteen parameters, whereas Func returns a value and accommodates one to sixteen parameters, with the last parameter representing the return type.

Regarding usage, Action is suitable when requiring an action without returning a value, while Func is preferable for actions combined with returning a value.

Practical use cases for Action include updating UI elements in a desktop application and logging information without expecting a return value. In contrast, Func is commonly employed for performing calculations and returning the result, as well as querying a database and returning the fetched data.

Conclusion

Understanding the differences between Action and Func delegates in C# is crucial for writing clear, modular, and efficient code. While Action is suitable for scenarios where an action without returning a value is needed, Func is ideal when both an action and a return value are required. Leveraging these delegates in our C# applications can lead to more maintainable and readable code.

Top comments (0)