English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Predicate is like Func and Action delegates. It represents a method that defines a set of conditions and determines whether a specified object meets these conditions. This delegate is used by several methods of the Array and List classes to search for elements in a collection. The Predicate delegate method must take one input parameter and return a boolean value true or false.
The Predicate delegate is defined in the System namespace as follows:
Predicate signature:
public delegate bool Predicate<in T>(T obj);
Like other delegate types, Predicate delegate can also be used with any method, anonymous method, or lambda expression.
static bool IsUpperCase(string str) { return str.Equals(str.ToUpper()); } static void Main(string[] args) { Predicate<string> isUpper = IsUpperCase; bool result = isUpper("hello world!!"); Console.WriteLine(result); }
false
You can also assign an anonymous method to the Predicate delegate type as shown below.
static void Main(string[] args) { Predicate<string> isUpper = delegate(string s) { return s.Equals(s.ToUpper()); }; bool result = isUpper("hello world!!"); }
You can also assign a lambda expression to the Predicate delegate type as shown below.
static void Main(string[] args) { Predicate<string> isUpper = s => s.Equals(s.ToUpper()); bool result = isUpper("hello world!!"); }
Predicate delegate is a generic delegate that returns a bool type
Predicate<int> delegate represents a delegate that takes an int parameter and returns a bool
Predicate delegates have exactly one parameter and return a fixed bool value
Predicate delegates take one input parameter and return a boolean value.
Anonymous methods and Lambda expressions can be assigned to predicate delegates.