Func, Action, Predicate
In this page:
Func, Action, Predicate
Func<T,...,TResult> represents a method that returns a value, Action<T,...> represents one that returns nothing, and Predicate<T> represents one that takes a T and returns bool. These built-in generic delegate types save you from declaring a custom delegate type for common shapes. They're used extensively throughout LINQ and collection methods.
Note: Func<int, int, int> means "takes two ints, returns an int" — the last type parameter is always the return type.
Example: Func, Action, Predicate
using System;
class Program
{
static void Main(string[] args)
{
Func<int, int, int> add = (a, b) => a + b;
Action<string> print = msg => Console.WriteLine(msg);
Predicate<int> isEven = n => n % 2 == 0;
Console.WriteLine(add(4, 5));
print("Hello from Action");
Console.WriteLine(isEven(10));
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: