Predicate(Of T) Delegate
Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- obj
-
Type:
T
The object to compare against the criteria defined within the method represented by this delegate.
Return Value
Type: System.Booleantrue if obj meets the criteria defined within the method represented by this delegate; otherwise, false.
Type Parameters
- In T
The type of the object to compare.
This delegate is used by several methods of the Array and List(Of T) classes to search for elements in the collection.
Typically, the Predicate(Of 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(Of T).FindAll method is able to return a HockeyTeam object for each team founded on or before that year.
Imports System.Collections.Generic Public Class HockeyTeam Private _name As String Private _founded As Integer Public Sub New(name As String, year As Integer) _name = name _founded = year End Sub Public ReadOnly Property Name As String Get Return _name End Get End Property Public ReadOnly Property Founded As Integer Get Return _founded End Get End Property End Class Module Example Public Sub Main() Dim rnd As New Random() Dim teams As New List(Of HockeyTeam)() teams.AddRange( { new HockeyTeam("Detroit Red Wings", 1926), new HockeyTeam("Chicago Blackhawks", 1926), new HockeyTeam("San Jose Sharks", 1991), new HockeyTeam("Montreal Canadiens", 1909), new HockeyTeam("St. Louis Blues", 1967) } ) Dim years() As Integer = { 1920, 1930, 1980, 2000 } Dim foundedBeforeYear As Integer = years(rnd.Next(0, years.Length)) Console.WriteLine("Teams founded before {0}:", foundedBeforeYear) For Each team in teams.FindAll( Function(x) x.Founded <= foundedBeforeYear ) Console.WriteLine("{0}: {1}", team.Name, team.Founded) Next End Sub End Module ' The example displays output similar to the following: ' Teams founded before 1930: ' Detroit Red Wings: 1926 ' Chicago Blackhawks: 1926 ' Montreal Canadiens: 1909
The following code example uses a Predicate(Of T) delegate with the Array.Find(Of T) method to search an array of Point structures. The example explicitly defines a Predicate(Of 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(Of T), as the second example illustrates.
Imports System.Drawing Public Class Example Public Shared Sub Main() ' Create an array of Point structures. Dim points() As Point = { new Point(100, 200), new Point(150, 250), new Point(250, 375), new Point(275, 395), new Point(295, 450) } ' Define the Predicate(Of T) delegate. Dim predicate As Predicate(Of Point) = AddressOf Example.FindPoints ' Find the first Point structure for which X times Y ' is greater than 100000. Dim first As Point = Array.Find(points, predicate) ' Display the first structure found. Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y) End Sub Private Shared Function FindPoints(obj As Point) As Boolean Return obj.X * obj.Y > 100000 End Function End Class ' The example displays the following output: ' Found: X = 275, Y = 395
The following example is identical to the previous example, except that it uses a lambda expression to represent the Predicate(Of 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.
Imports System.Drawing Public Class Example Public Shared Sub Main() ' Create an array of Point structures. Dim points() As Point = { new Point(100, 200), new Point(150, 250), new Point(250, 375), new Point(275, 395), new Point(295, 450) } ' Find the first Point structure for which X times Y ' is greater than 100000. Dim first As Point = Array.Find(points, Function(x) x.X * x.Y > 100000 ) ' Display the first structure found. Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y) End Sub End Class ' The example displays the following output: ' Found: X = 275, Y = 395
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1