Func, Action and Predicate
Func, Action and Predicate
Func, Action and Predicate are standard generic delegate families. Choose by the callable contract: a returned value, no returned value, or a Boolean decision about one input.
Examples target the proposed C# 14 / .NET 10 console baseline. Each is a separate Program.cs; outputs are predictions pending compilation.
Select by input and output types
Func<T,TResult> accepts a T and returns TResult; the last type argument is the result. Func<TResult> takes no arguments. Action<T> accepts a T and returns void; Action takes no arguments. Predicate<T> takes one T and returns bool. A Predicate<int> and Func<int,bool> are distinct delegate types even though the same method or lambda can fit both.
using System;Func<int, int> bonus = score => score + 5;Action<int> report = score => Console.WriteLine("Score " + score);Predicate<int> passed = score => score >= 60;int updated = bonus(55);report(updated);Console.WriteLine(passed(updated));The Func produces a value, Action reports it, and Predicate decides whether it meets the threshold.
Expected output (reasoned, not executed):
Score 60TrueBuild a configurable filtering operation
A List<T>.FindAll accepts a Predicate<T> and returns a new list with matching elements. Use an ordinary foreach to build a custom filter when you want to understand the algorithm or accept a different callback contract. The source order is preserved in these examples and no LINQ prerequisites are required.
using System;using System.Collections.Generic;var scores = new List<int> { 45, 70, 60, 59 };Predicate<int> passing = score => score >= 60;var selected = scores.FindAll(passing);Console.WriteLine(string.Join(",", selected));Console.WriteLine(scores.Count);Console.WriteLine(new List<int>().FindAll(passing).Count);FindAll returns matches in source order without changing scores. Empty input produces an empty result.
Expected output (reasoned, not executed):
70,6040Compose a filter, transform and reporting step
A callback-based pipeline makes the order of operations explicit. The filter examines original values; the map creates labels for matches; the sink receives each label. Func and Action support reference-type variance in compatible directions, but matching signatures alone do not make different named delegate types interchangeable. To bridge a Predicate<T> to Func<T,bool>, use value => predicate(value) or a compatible method group.
using System;using System.Collections.Generic;Process(new[] { 0, 3, 5 }, n => n > 0, n => $"Available: {n}", Console.WriteLine);static void Process(IEnumerable<int> input, Func<int, bool> include, Func<int, string> label, Action<string> report){ foreach (int value in input) if (include(value)) report(label(value));}The filter runs once per input, and only accepted values are transformed and reported. The caller can change each operation independently.
Expected output (reasoned, not executed):
Available: 3Available: 5Common mistakes and edge cases
Place the result type last in Func. Action is not a value-returning function. Predicate is not implicitly assignable to Func merely because their signatures match. Do not hide side effects in a filter unless the contract explicitly calls for them. Exceptions from callbacks propagate unless handled.
Practice and next steps
Implement a pipeline for [0,4,8,-2] that accepts positive values, doubles them, and reports a label for each. Test an empty input and a threshold excluding every item. Explain why the reporting callback uses Action<string>.
Next: Events and subscriptions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Func, Action and Predicate — knowledge check
Apply the lesson concepts to the following scenarios.
What is the result type of Func<int,string>?
Which delegate fits a method accepting a string and returning void?
Which type matches List<int>.FindAll directly?
Can a Predicate<int> variable be directly assigned to Func<int,bool> just because signatures match?
A filter rejects all inputs. How many times does the report callback run in Process?