Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Predicate(T) Delegate
Collapse the table of content
Expand the table of content

Predicate(T) Delegate

Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

type Predicate = 
    delegate of  
        obj:'T -> bool

Type Parameters

in T

The type of the object to compare.

This type parameter is contravariant. That is, you can use either the type you specified or any type that is less derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.

Parameters

obj
Type: T

The object to compare against the criteria defined within the method represented by this delegate.

Return Value

Type: System.Boolean
true if obj meets the criteria defined within the method represented by this delegate; otherwise, false.

This delegate is used by several methods of the Array and List(T) classes to search for elements in the collection.

Typically, the Predicate(T) delegate is represented by a lambda expression. Because locally scoped variables are available to the lambda expression, it is easy to test for a condition that is not precisely known at compile time. This is simulated in the following example, which defines a HockeyTeam class that contains information about a National Hockey League team and the year in which it was founded. The example defines an array of integer values that represent years, and randomly assigns one element of the array to foundedBeforeYear, which is a variable that is locally scoped to the example's Main method. Because locally scoped variables are available to a lambda expression, the lambda expression passed to the List(T).FindAll method is able to return a HockeyTeam object for each team founded on or before that year.

No code example is currently available or this language may not be supported.

The following code example uses a Predicate(T) delegate with the Array.Find(T) method to search an array of Point structures. The example explicitly defines a Predicate(T) delegate named predicate and assigns it a method named FindPoints that returns true if the product of the Point.X and Point.Y fields is greater than 100,000. Note that it is customary to use a lambda expression rather than to explicitly define a delegate of type Predicate(T), as the second example illustrates.

No code example is currently available or this language may not be supported.

The following example is identical to the previous example, except that it uses a lambda expression to represent the Predicate(T) delegate. Each element of the points array is passed to the lambda expression until the expression finds an element that meets the search criteria. In this case, the lambda expression returns true if the product of the X and Y fields is greater than 100,000.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft