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. 委派所表示的方法ProductGT10,傳回true產品 X 和 Y 欄位是否大於 100,000。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

而不是明確定義所需的簽章的方法具現化Predicate<T>委派,並傳遞至委派Find方法,它是慣用使用 lambda 運算式。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

備註

Predicate<T>是一種方法或 lambda 運算式傳回的委派true物件傳遞給是否符合委派或 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.

這個方法是 O (n) 運算,其中nLengtharrayThis method is an O(n) operation, where n is the Length of array.

適用於

另請參閱