0
0
CsharpComparisonBeginner · 4 min read

Func vs Action vs Predicate in C#: Key Differences and Usage

In C#, 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.

FeatureFuncActionPredicate
Return TypeReturns a value (any type)Returns void (no value)Returns bool
Parameters0 or more input parameters0 or more input parametersExactly 1 input parameter
PurposeCompute and return a resultPerform an operation without returningTest a condition and return true/false
NamespaceSystemSystemSystem
Example UsageCalculate sum, get string lengthPrint to console, update UICheck 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

csharp
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));
    }
}
Output
Func result: 7 Action output: Hello World Predicate result for 4: True
↔️

Predicate Equivalent

csharp
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));
    }
}
Output
Func as Predicate result for 4: True
🎯

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.
Use Predicate for simple true/false tests on one input.
Use Action for operations without return values.