English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Predicate delegate

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.

Definition of Predicate delegate

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);
}
Output:
false

Predicate delegate and anonymous method

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!!");
}

Predicate delegate and Lambda expression

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!!");
}

Usage Instructions for Predicate Delegate

  • 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

Key Points to Remember

  1. Predicate delegates take one input parameter and return a boolean value.

  2. Anonymous methods and Lambda expressions can be assigned to predicate delegates.