C# Predicate Type

Lambda expression syntax

A Predicate returns true or false. The Predicate type in the C# language stores a method that receives one parameter and returns one value of true or false. And it is often used with lambda expression syntax.

This C# example program uses the Predicate type. A Predicate returns a bool.

Example

Note

We look at a program that uses two Predicate instances with the parameterized type of integer. The return value of Predicate types is always bool, meaning true or false must be the result. The Predicate types are assigned to lambda expressions. The program then demonstrates how the Invoke method always returns true or false when it is called in the source text.

Program that uses Predicate type instances [C#]

using System;

class Program
{
    static void Main()
    {
	//
	// This Predicate instance returns true if the argument is one.
	//
	Predicate<int> isOne =
	    x => x == 1;
	//
	// This Predicate returns true if the argument is greater than 4.
	//
	Predicate<int> isGreaterEqualFive =
	    (int x) => x >= 5;

	//
	// Test the Predicate instances with various parameters.
	//
	Console.WriteLine(isOne.Invoke(1));
	Console.WriteLine(isOne.Invoke(2));
	Console.WriteLine(isGreaterEqualFive.Invoke(3));
	Console.WriteLine(isGreaterEqualFive.Invoke(10));
    }
}

Output

True
False
False
True
Main method

Description. This program text is contained in a compilation unit. It defines the Main entry point, where two Predicate instances with the parameterized type int are declared and assigned.

The lambda expression syntax is used to specify the function body for the Predicate instances. The => syntax separates the arguments of the lambda expressions from the method body. The right-hand side of each lambda expression is evaluated to a Boolean result.

True keyword

In lambda expressions, the right-hand side of the expression acquires an implicit return value if this is indicated by the context. The Predicate type must always have a Boolean return value, and the two above lambdas return true if the argument is one, and if the argument is greater than four, respectively. Finally the program executes the Invoke instance method on the Predicate types to demonstrate the functionality.

List

List type.

The Predicate type is often used with the List generic type in the C# programming language. You can specify a lambda expression as the argument to a List method that receives a Predicate type. The C# compiler then uses type inference to resolve the lambda expression to the correct Predicate type. Alternatively you can store the Predicate in a field or local variable.

RemoveAll List Method List Find Method

Note: Please see the articles on the Find and RemoveAll methods for details.

SICP

Structure and Interpretation of Computer Programs

The concept of predicate functions is explored in the book Structure and Interpretation of Computer Programs. In the book, predicates are described as functions that must return true or false. This is the core meaning of the term predicate when describing functions; the type Predicate in the .NET Framework adheres to this definition, but is just a specific implementation.

Structure and Interpretation of Computer Programs

Summary

The C# programming language

We looked at the Predicate type. We saw how it is applied correctly to lambda expressions where the result value is of type bool. The Predicate type must always receive one parameterized type, and the method body on the right-hand side of the lambda expression is evaluated in a boolean context. And the term predicate in general has wide applicability in computer science.

Delegate Tutorial
.NET