閱讀英文

共用方式為


Predicate<T> 代理人

定義

代表定義一組準則的方法,並判斷指定的物件是否符合這些準則。

C#
public delegate bool Predicate<in T>(T obj);
C#
public delegate bool Predicate<T>(T obj);

類型參數

T

要比較之物件的型別。

這是反變數的型別參數。 也就是說,您可以使用您指定的類型,或衍生程度較低的任何類型。 如需共變數與反變數的詳細資訊,請參閱泛型中的共變數與反變數

參數

obj
T

要比較的物件,根據這個委派表示方法中所定義的準則。

傳回值

Boolean

如果 obj 符合這個委派表示方法中所定義的準則,則為 true,否則為 false

範例

下列程式碼範例會搭配 方法使用 Predicate<T> 委派 Array.Find 來搜尋結構的陣列 Point 。 此範例會明確定義 Predicate<T> 名為 predicate 的委派,並為其指派名為 FindPoints 的方法,如果 和 Point.Y 欄位的 Point.X 乘積大於 100,000,則傳回 true 此方法。 請注意,使用 Lambda 運算式而不是明確定義 類型的 Predicate<T> 委派是自訂的,如第二個範例所示。

C#
using System;
using System.Drawing;

public class Example
{
   public static void Main()
   {
      // Create an array of Point structures.
      Point[] points = { new Point(100, 200),
                         new Point(150, 250), new Point(250, 375),
                         new Point(275, 395), new Point(295, 450) };

      // Define the Predicate<T> delegate.
      Predicate<Point> predicate = FindPoints;

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

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

   private static bool FindPoints(Point obj)
   {
      return obj.X * obj.Y > 100000;
   }
}
// The example displays the following output:
//        Found: X = 275, Y = 395

下列範例與上一個範例相同,不同之處在于它會使用 Lambda 運算式來表示 Predicate<T> 委派。 陣列的每個元素 points 都會傳遞至 Lambda 運算式,直到運算式找到符合搜尋準則的專案為止。 在此情況下,如果 X 和 Y 欄位的乘積大於 100,000,則 Lambda 運算式會傳回 true

C#
using System;
using System.Drawing;

public class Example
{
   public static void Main()
   {
      // Create an array of 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, x => x.X * x.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

備註

和 類別的 Array List<T> 數種方法會使用此委派來搜尋集合中的專案。

一般而言, Predicate<T> 委派是由 Lambda 運算式表示。 因為 Lambda 運算式可以使用本機範圍變數,所以很容易在編譯時期測試不精確已知的條件。 這在下列範例中模擬,其定義 HockeyTeam 類別,其中包含國家 Hockey 聯盟小組及其建立年份的相關資訊。 此範例會定義代表年份的整數值陣列,並隨機將陣列的一個專案指派給 foundedBeforeYear ,這是本機範圍設定為範例方法的 Main 變數。 因為 Lambda 運算式可以使用本機範圍變數,所以傳遞至 List<T>.FindAll 方法的 Lambda 運算式可以傳回 HockeyTeam 在該年或之前找到的每個小組物件。

C#
using System;
using System.Collections.Generic;

public class HockeyTeam
{
   private string _name;
   private int _founded;

   public HockeyTeam(string name, int year)
   {
      _name = name;
      _founded = year;
   }

   public string Name {
      get { return _name; }
   }

   public int Founded {
      get { return _founded; }
   }
}

public class Example
{
   public static void Main()
   {
      Random rnd = new Random();
      List<HockeyTeam> teams = new List<HockeyTeam>();
      teams.AddRange( new HockeyTeam[] { new HockeyTeam("Detroit Red Wings", 1926),
                                         new HockeyTeam("Chicago Blackhawks", 1926),
                                         new HockeyTeam("San Jose Sharks", 1991),
                                         new HockeyTeam("Montreal Canadiens", 1909),
                                         new HockeyTeam("St. Louis Blues", 1967) } );
      int[] years = { 1920, 1930, 1980, 2000 };
      int foundedBeforeYear = years[rnd.Next(0, years.Length)];
      Console.WriteLine("Teams founded before {0}:", foundedBeforeYear);
      foreach (var team in teams.FindAll( x => x.Founded <= foundedBeforeYear))
         Console.WriteLine("{0}: {1}", team.Name, team.Founded);
   }
}
// The example displays output similar to the following:
//       Teams founded before 1930:
//       Detroit Red Wings: 1926
//       Chicago Blackhawks: 1926
//       Montreal Canadiens: 1909

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱