Nota: este delegado es nuevo en la versión 2.0 de .NET Framework.
Representa el método que define un conjunto de criterios y determina si el objeto especificado cumple esos criterios.
Espacio de nombres: System
Ensamblado: mscorlib (en mscorlib.dll)
Visual Basic (Declaración)
Public Delegate Function Predicate(Of T) ( _
obj As T _
) As Boolean
Dim instance As New Predicate(Of T)(AddressOf HandlerMethod)
public delegate bool Predicate<T> (
T obj
)
generic<typename T>
public delegate bool Predicate (
T obj
)
J# admite el uso de métodos y tipos genéricos, pero no admite la declaración de métodos y tipos nuevos.
JScript no admite el uso de métodos y tipos genéricos.
Parámetros de tipo
- T
Tipo del objeto que se va a comparar.
Parámetros
- obj
Objeto que se va a comparar según los criterios definidos en el método representado por este delegado.
Valor devuelto
Es true si obj cumple los criterios definidos en el método representado por este delegado; de lo contrario, es false.
Varios métodos de las clases Array y List utilizan este delegado para buscar elementos en la colección.
En el ejemplo de código siguiente se utiliza un delegado Predicate con el método Array.Find para buscar en una matriz de estructuras Point. El método representado por el delegado, ProductGT10, devuelve true si el producto de los campos X e Y es mayor que 100.000. El método Find llama al delegado para cada elemento de la matriz, deteniéndose en el primer punto que cumpla la condición de prueba.
Nota |
|---|
| Los usuarios de Visual Basic y C# no necesitan crear el delegado explícitamente, ni especificar el argumento de tipo del método genérico. Los compiladores determinan los tipos necesarios a partir de los argumentos de método que suministre. |
Imports System
Imports System.Drawing
Public Class Example
Public Shared Sub Main()
' Create an array of five 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) }
' To find the first Point structure for which X times Y
' is greater than 100000, pass the array and a delegate
' that represents the ProductGT10 method to the Shared
' Find method of the Array class.
Dim first As Point = Array.Find(points, _
AddressOf ProductGT10)
' Note that you do not need to create the delegate
' explicitly, or to specify the type parameter of the
' generic method, because the compiler has enough
' context to determine that information for you.
' Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", _
first.X, first.Y)
End Sub
' This method implements the test condition for the Find
' method.
Private Shared Function ProductGT10(ByVal p As Point) As Boolean
If p.X * p.Y > 100000 Then
Return True
Else
Return False
End If
End Function
End Class
' This code example produces the following output:
'
'Found: X = 275, Y = 395
using System;
using System.Drawing;
public class Example
{
public static void Main()
{
// Create an array of five Point structures.
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };
// To find the first Point structure for which X times Y
// is greater than 100000, pass the array and a delegate
// that represents the ProductGT10 method to the Shared
// Find method of the Array class.
Point first = Array.Find(points, ProductGT10);
// Note that you do not need to create the delegate
// explicitly, or to specify the type parameter of the
// generic method, because the C# compiler has enough
// context to determine that information for you.
// Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
}
// This method implements the test condition for the Find
// method.
private static bool ProductGT10(Point p)
{
if (p.X * p.Y > 100000)
{
return true;
}
else
{
return false;
}
}
}
/* This code example produces the following output:
Found: X = 275, Y = 395
*/
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition
.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.
.NET Framework
Compatible con: 2.0
.NET Compact Framework
Compatible con: 2.0