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.YPoint.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 表达式,直到表达式找到满足搜索条件的元素。 在这种情况下,lambda 表达式返回 true X 和 Y 字段的乘积大于 100,000。

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

注解

此委托由多个方法和类用于ArrayList<T>搜索集合中的元素。

通常, Predicate<T> 委托由 lambda 表达式表示。 由于本地范围的变量对 lambda 表达式可用,因此可以轻松测试编译时无法准确知道的条件。 以下示例中模拟了这一点,该类定义一个 HockeyTeam 类,其中包含有关国家曲棍球联赛球队的信息以及创建该团队的年份。 该示例定义表示年份的整数值的数组,并随机将数组的一个元素分配给该数组,该数组 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

另请参阅