Predicate vs Func in C#
Article

Predicate vs Func in C#


In C#, a predicate is a delegate (a type that refers to a method) or a lambda expression that takes a single parameter and returns a Boolean value indicating whether the input meets certain criteria.

For example, the code below defines a predicate that tests if a given integer is even:

Predicate<int> isEven = n => n % 2 == 0; 
bool result = isEven(4); // true

A func, on the other hand, is a similar type of delegate or lambda expression, but it can take any number of input parameters and return any type of value.

For example, the code below defines a func that takes two integers and returns their sum:

Func<int, int, int> sum = (x, y) => x + y; 
int result = sum(3, 4); // 7

If you want to learn more about Func and Predicate in C# please check out the video tutorials below:

Image by vector_corp on Freepik

Keep learning

Full-length .NET courses on every platform I teach on.

Share this article