Home
Map
Predicate ExamplesUse the Predicate type, which is a function type that always returns a bool value to mean true or false.
C#
This page was last reviewed on May 10, 2023.
Predicate. A Predicate returns true or false. This C# type stores a method that receives 1 parameter and returns 1 value of true or false.
return
true, false
This type is often used with lambda expression syntax. We can create a predicate by specifying a lambda expression and passing it to a method.
Lambda
An example. This program that uses 2 Predicate instances with the parameterized type of integer. The return value of Predicate types is always bool (true or false).
Part 1 We create a Predicate by assigning to a lambda expression. The predicate has not yet been called.
Part 2 We create a second Predicate, isGreaterEqualFive, and it has an int argument called "x."
Part 3 The program demonstrates how the Invoke method always returns true or false when it is called.
using System; // Part 1: this Predicate instance returns true if the argument is one. Predicate<int> isOne = x => x == 1; // Part 2: this Predicate returns true if the argument is greater than 4. Predicate<int> isGreaterEqualFive = (int x) => x >= 5; // Part 3: 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));
True False False True
List. Predicate is often used with the List generic type. You can specify a lambda expression as the argument to certain List methods. The C# compiler then uses type inference.
List
List RemoveAll
List Find, Exists
Step 1 We create a string array of 3 elements. Each string in the array has at least 4 characters.
Step 2 We pass a lambda expression to TrueForAll, which receives a Predicate instance. The return value is a bool.
Result The Predicate returns true if the string has 3 or more chars. This is true for all items in the array, so TrueForAll returns true.
using System; using System.Collections.Generic; // Step 1: create list. var items = new List<string> { "compiler", "interpreter", "file" }; // Step 2: use TrueForAll with a predicate. if (items.TrueForAll(item => item.Length >= 3)) { Console.WriteLine("PREDICATE TrueForAll"); }
PREDICATE TrueForAll
The right-hand side of a lambda acquires an implicit return value if this is indicated by the context. A Predicate must always have a Boolean return value.
The term "predicate" has wide applicability in computer science. In C#, predicates are specified with lambda expressions where the result value is of type bool.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 10, 2023 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.