Func vs Action vs Predicate in C#: Key Differences and Usage
Func represents a delegate that returns a value and can have zero or more input parameters, Action is a delegate that returns void and can have zero or more input parameters, and Predicate is a specialized Func that takes one input and returns a bool to test conditions.Quick Comparison
Here is a quick table to compare Func, Action, and Predicate based on their key characteristics.
| Feature | Func | Action | Predicate |
|---|---|---|---|
| Return Type | Returns a value (any type) | Returns void (no value) | Returns bool |
| Parameters | 0 or more input parameters | 0 or more input parameters | Exactly 1 input parameter |
| Purpose | Compute and return a result | Perform an operation without returning | Test a condition and return true/false |
| Namespace | System | System | System |
| Example Usage | Calculate sum, get string length | Print to console, update UI | Check if number is even |
Key Differences
Func is a delegate type designed to represent methods that return a value. It can take zero or more input parameters, and the last generic type parameter always specifies the return type. For example, Func<int, int, int> represents a method taking two integers and returning an integer.
Action is similar but is used for methods that do not return a value (void). It can also take zero or more input parameters but has no return type. For example, Action<string> represents a method that takes a string and returns nothing.
Predicate is a special case of Func that always takes exactly one input parameter and returns a bool. It is mainly used for testing conditions, such as filtering or searching. For example, Predicate<int> represents a method that takes an integer and returns true or false.
Code Comparison
using System; class Program { static void Main() { // Func example: takes two ints, returns their sum Func<int, int, int> add = (a, b) => a + b; Console.WriteLine("Func result: " + add(3, 4)); // Action example: takes a string, prints it Action<string> print = message => Console.WriteLine("Action output: " + message); print("Hello World"); // Predicate example: takes an int, returns true if even Predicate<int> isEven = num => num % 2 == 0; Console.WriteLine("Predicate result for 4: " + isEven(4)); } }
Predicate Equivalent
using System; class Program { static void Main() { // Predicate equivalent using Func Func<int, bool> isEvenFunc = num => num % 2 == 0; Console.WriteLine("Func as Predicate result for 4: " + isEvenFunc(4)); } }
When to Use Which
Choose Func when you need to run a method that returns a value, especially when you have multiple input parameters and want flexibility in return types.
Choose Action when you want to execute code that performs an operation but does not return any value, such as logging or updating UI.
Choose Predicate when you need a simple way to test a condition on a single input and get a true/false result, commonly used in filtering or searching collections.
Key Takeaways
Func returns a value and can have multiple inputs.Action returns void and can have multiple inputs.Predicate takes one input and returns a bool for conditions.Predicate for simple true/false tests on one input.Action for operations without return values.