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乘積大於100000,則委派所代表的方法會傳回。 trueThe 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

備註

是方法或 lambda 運算式的委派true ,會在傳遞給它的物件符合委派或 lambda 運算式中所定義的條件時傳回。 Predicate<T>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. 的專案Predicate<T>會個別傳遞至,從第一個元素開始,並以最後一個元素結束。 arrayThe 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.

適用於

另請參閱