# Array.Find<T>(T[], Predicate<T>) 方法

## 定义

``````public:
generic <typename T>
static T Find(cli::array <T> ^ array, Predicate<T> ^ match);``````
``public static T Find<T> (T[] array, Predicate<T> match);``
``public static T? Find<T> (T[] array, Predicate<T> match);``
``static member Find : 'T[] * Predicate<'T> -> 'T``
``Public Shared Function Find(Of T) (array As T(), match As Predicate(Of T)) As T``

T

array
T[]

match
Predicate<T>

T

#### 例外

`array``null``array` is `null`.

-or-

`match``null``match` is `null`.

## 示例

Visual Basic 和 c # 用户不必显式创建委托或指定泛型方法的类型参数。Visual Basic and C# users do not have to create the delegate explicitly or specify the type argument of the generic method. 编译器根据您提供的方法参数确定必要的类型。The compilers determine the necessary types from the method arguments you supply.

``````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) };

// Find the first Point structure for which X times Y
// is greater than 100000.
Point first = Array.Find(points, ProductGT10);

// Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
}

// Return true if X times Y is greater than 100000.
private static bool ProductGT10(Point p)
{
return p.X * p.Y > 100000;
}
}
// The example displays the following output:
//       Found: X = 275, Y = 395
``````
``````Imports System.Drawing

Public Module Example
Public 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) }

' Find the first Point structure for which X times Y
' is greater than 100000.
Dim first As Point = Array.Find(points, AddressOf ProductGT10)

' Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", _
first.X, first.Y)
End Sub

' Return true if X times Y is greater than 100000.
Private Function ProductGT10(ByVal p As Point) As Boolean
Return p.X * p.Y > 100000
End Function
End Module
' The example displays 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) };

// Find the first Point structure for which X times Y
// is greater than 100000.
Point first = Array.Find(points, p => p.X * p.Y > 100000);

// Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
}
}
// The example displays the following output:
//       Found: X = 275, Y = 395
``````
``````Imports System.Drawing

Public Module Example
Public 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) }

' Find the first Point structure for which X times Y
' is greater than 100000.
Dim first As Point = Array.Find(points,
Function(p) p.X * p.Y > 100000)

' Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", _
first.X, first.Y)
End Sub
End Module
' The example displays the following output:
'       Found: X = 275, Y = 395
``````

## 注解

Predicate<T> `true` 如果传递给它的对象与委托或 lambda 表达式中定义的条件相匹配，则为方法的委托或 lambda 表达式。The Predicate<T> is a delegate to a method or a lambda expression that returns `true` if the object passed to it matches the conditions defined in the delegate or lambda expression. 的元素 `array` 分别传递到 Predicate<T> ，从第一个元素开始，到最后一个元素结束。The elements of `array` are individually passed to the Predicate<T>, starting with the first element and ending with the last element. 找到匹配项时将停止处理。Processing is stopped when a match is found.