What are Action and Func Delegates?
In C#, a delegate points to one or more methods. Once you instantiate the delegate, the corresponding methods can be invoked. To associate a delegate with a particular method, the method must have the same return type and parameter type as that of the delegate.
Delegates in C# have some features that distinguish them from normal methods:
- Methods (including anonymous methods as a block of code) can be passed as parameters to a delegate
- A delegate can invoke multiple methods simultaneously (termed multicasting)
- A delegate can encapsulate static methods
- Delegates ensure type safety as the return and parameter types of the delegate are the same as those of the referenced method
Two commonly used delegates in C# are the Action and Func delegates. Let's explore what they are and how to use them.
Action Delegates in C#
The Action delegate is a built-in delegate type in C# representing a method that takes no parameters and returns void. It is commonly used for methods that perform an action without returning any value. The Action delegate can also be parameterized to accept up to 16 input parameters.
// Simple Action Delegate without parameters
Action Work = () => Console.WriteLine("Doing tasks");
Work(); // Invoking the action delegate
Func Delegates in C#
The Func delegate is a generic delegate type representing a method that takes parameters and returns a value. It is widely used for methods that perform a computation and return a result. The last type parameter specifies the return type of the delegate.
// Func Delegate
Func<float, float, float> Divide = (a, b) => a / b;
Console.WriteLine(Divide(5, 3)); // Displays: 1.666666
In this code, a lambda expression is used with the Func delegate so that the code is compact and crisp.
Why Use Action and Func Delegates?
Action Delegate
- Action delegates allow you to define methods concisely without the necessity to explicitly declare a custom delegate type
- They are ideal for scenarios where you must perform an action but don't require a return value
Func Delegate
- Provide a flexible way to define methods that can return different types of values
- Help in writing reusable code by separating the logic from the actual computation
How to Use Action and Func Delegates?
Using Action Delegate
// Action Delegate with parameter
Action<string> sayHi = lang => Console.WriteLine($"Hi, {user}, glad to meet you!");
sayHi("Sarah"); // Displays: Hi, Sarah, glad to meet you!
Here, the user is passed as an argument to the delegate through a lambda expression. Based on the value present in the user, the output will differ.
Using Func Delegate
// Instantiate delegate to reference Titlecase method
Func<string, string> convertMethod = Titlecase;
var name = "illinois";
// Use delegate instance to call Titlecase method
Console.WriteLine(convertMethod(name));
string Titlecase(string inputString)
{
var textinfo = new CultureInfo("en-US", false).TextInfo;
return textinfo.ToTitleCase(inputString);
}
Where to Use Action and Func Delegates?
Action Delegate
- Event Handling: Use in scenarios where an action needs to be triggered in response to an event.
- Parallel Programming: Employ parallel programming when performing actions on separate threads.
Func Delegate
- Data Transformation: Utilize methods that transform input data into a desired output.
- Conditional Logic: Apply in scenarios where a method needs to return a value based on some conditions.
Conclusion
Action and Func delegates in C# provide a convenient way to work with methods as parameters. While the Action delegate is suitable for methods that perform actions, the Func delegate is versatile, handling methods that return values. Understanding when and how to use these delegates enhances the readability and flexibility of your C# code.