Array.Find(T[], Predicate<T>) Array.Find(T[], Predicate<T>) Array.Find(T[], Predicate<T>) Array.Find(T[], Predicate<T>) Method

定义

搜索与指定谓词所定义的条件相匹配的元素,并返回整个 Array 中的第一个匹配元素。Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.

public:
generic <typename T>
 static T Find(cli::array <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

数组元素的类型。The type of the elements of the array.

参数

array
T[]

要搜索的从零开始的一维数组。The one-dimensional, zero-based array to search.

match
Predicate<T> Predicate<T> Predicate<T> Predicate<T>

用于定义要搜索的元素的条件的谓词。The predicate that defines the conditions of the element to search for.

返回

T T T T

如果找到与指定谓词定义的条件匹配的第一个元素,则为该元素;否则为类型 T 的默认值。The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

异常

arraynullarray is null.

- 或 --or- matchnullmatch is null.

示例

下面的示例将Predicate<T>委托Find与泛型方法一起使用Point以搜索结构数组。The following example uses a Predicate<T> delegate with the Find generic method to search an array of Point structures. 如果 X 和 Y 字段的ProductGT10乘积大于true 100000, 则委托表示的方法返回。The method the delegate represents, ProductGT10, returns true if the product of the X and Y fields is greater than 100,000. Find方法为数组的每个元素调用委托, 并返回满足测试条件的第一个点。The Find method calls the delegate for each element of the array, returning the first point that meets the test condition.

备注

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

可以使用 lambda 表达式, 而不是使用必需的签名显式Predicate<T>定义方法, 实例化委托并将Find委托传递给方法。Rather than explicitly defining a method with the necessary signature, instantiating a Predicate<T> delegate, and passing the delegate to the Find method, it is customary to use a lambda expression. 下面的示例与上一个示例相同, 不同之处在于它使用 lambda 表达式作为match参数。The following example is identical to the previous one, except that it uses a lambda expression as the match argument.

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

注解

如果传递给它的对象与委托或 lambda 表达式中定义的条件相匹配, 则为方法的委托或lambda表达式。Predicate<T> trueThe 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.

此方法为 On() 运算, 其中nLengtharrayThis method is an O(n) operation, where n is the Length of array.

适用于

另请参阅