Predicate<T> Predicate<T> Predicate<T> Predicate<T> Delegate

정의

조건 집합을 정의하고 지정된 개체가 이러한 조건을 충족하는지 여부를 확인하는 메서드를 나타냅니다.Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.

generic <typename T>
public delegate bool Predicate(T obj);
public delegate bool Predicate<in T>(T obj);
type Predicate<'T> = delegate of 'T -> bool
Public Delegate Function Predicate(Of In T)(obj As T) As Boolean 

형식 매개 변수

T

비교할 개체의 형식입니다.The type of the object to compare.

매개 변수

obj

이 대리자가 나타내는 메서드에 정의된 조건과 비교할 개체입니다.The object to compare against the criteria defined within the method represented by this delegate.

반환 값

System.Boolean

obj가 이 대리자가 나타내는 메서드에 정의된 조건을 충족하면 true이고, 그렇지 않으면 false입니다.true if obj meets the criteria defined within the method represented by this delegate; otherwise, false.

상속
Predicate<T>Predicate<T>Predicate<T>Predicate<T>

예제

다음 코드 예제에서는 Predicate<T> 사용 하 여 대리자를 Array.Find 배열을 검색 하는 방법 Point 구조입니다.The following code example uses a Predicate<T> delegate with the Array.Find method to search an array of Point structures. 이 예제에서는 명시적으로 정의 합니다.를 Predicate<T> 라는 대리자 predicate 라는 메서드를 할당 FindPoints 반환 하는 true 경우를 곱한 값를 Point.XPoint.Y 필드 100,000 보다 큽니다.The example explicitly defines a Predicate<T> delegate named predicate and assigns it a method named FindPoints that returns true if the product of the Point.X and Point.Y fields is greater than 100,000. 일반적인 형식의 대리자를 명시적으로 정의 하기 보다는 람다 식을 사용 하는 Predicate<T>처럼 두 번째 예제를 보여 줍니다.Note that it is customary to use a lambda expression rather than to explicitly define a delegate of type Predicate<T>, as the second example illustrates.

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
Imports System.Drawing

Public Class Example
   Public Shared Sub Main()

      ' Create an array of 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) }
      
      ' Define the Predicate(Of T) delegate.
      Dim predicate As Predicate(Of Point) = AddressOf Example.FindPoints
      
      ' Find the first Point structure for which X times Y  
      ' is greater than 100000. 
      Dim first As Point = Array.Find(points, predicate)

      ' Display the first structure found.
      Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y)
   End Sub 
   
   Private Shared Function FindPoints(obj As Point) As Boolean
      Return obj.X * obj.Y > 100000
   End Function

End Class 
' The example displays the following output:
'       Found: X = 275, Y = 395

다음 예제는 람다 식을 나타내는 데 사용 하 여 이전 예제와 동일 합니다 Predicate<T> 위임 합니다.The following example is identical to the previous example, except that it uses a lambda expression to represent the Predicate<T> delegate. 각 요소는 points 식 검색 기준을 충족 하는 요소를 찾을 때까지 배열은 람다 식에 전달 됩니다.Each element of the points array is passed to the lambda expression until the expression finds an element that meets the search criteria. 이 경우 람다 식을 반환 true X 및 Y 필드의 곱이 100,000 보다 큰 경우.In this case, the lambda expression returns true if the product of the X and Y fields is greater than 100,000.

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
Imports System.Drawing

Public Class Example
   Public Shared Sub Main()

      ' Create an array of 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(x) x.X * x.Y > 100000 )

      ' Display the first structure found.
      Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y)
   End Sub 
End Class 
' The example displays the following output:
'       Found: X = 275, Y = 395

설명

이 대리자는의 몇 가지 방법으로 사용 합니다 ArrayList<T> 컬렉션의 요소를 검색 하는 클래스.This delegate is used by several methods of the Array and List<T> classes to search for elements in the collection.

일반적으로 Predicate<T> 대리자 람다 식을 사용 하 여 표시 됩니다.Typically, the Predicate<T> delegate is represented by a lambda expression. 로컬 범위 변수를 람다 식에 사용할 수 있으므로 정확 하 게 컴파일 타임에 알려지지 않은 조건에 대 한 테스트 하기 쉽습니다.Because locally scoped variables are available to the lambda expression, it is easy to test for a condition that is not precisely known at compile time. 이 정의 하는 다음 예제에서는 시뮬레이션 된 HockeyTeam National Hockey League 팀과 이전에 설립 된 연도 대 한 정보를 포함 하는 클래스입니다.This is simulated in the following example, which defines a HockeyTeam class that contains information about a National Hockey League team and the year in which it was founded. 예제에서는 연도 나타내는 정수 값의 배열을 정의 하 고 배열의 요소가 하나를 임의로 할당 foundedBeforeYear에 로컬로 예제의 범위가 지정 된 변수는 Main 메서드.The example defines an array of integer values that represent years, and randomly assigns one element of the array to foundedBeforeYear, which is a variable that is locally scoped to the example's Main method. 범위가 지정 된 로컬 변수를 람다 식에 사용할 수 있는 람다 식에 전달 합니다 List<T>.FindAll 메서드는 반환할 수 있습니다는 HockeyTeam 또는 해당 년 전에 설립 된 각 팀에 대 한 개체입니다.Because locally scoped variables are available to a lambda expression, the lambda expression passed to the List<T>.FindAll method is able to return a HockeyTeam object for each team founded on or before that year.

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
Imports System.Collections.Generic

Public Class HockeyTeam
   Private _name As String
   Private _founded As Integer
   
   Public Sub New(name As String, year As Integer)
      _name = name
      _founded = year
   End Sub

   Public ReadOnly Property Name As String
      Get
         Return _name
      End Get
   End Property

   Public ReadOnly Property Founded As Integer
      Get 
         Return _founded
      End Get   
   End Property
End Class

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      Dim teams As New List(Of HockeyTeam)()
      teams.AddRange( { 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) } )
      Dim years() As Integer = { 1920, 1930, 1980, 2000 }
      Dim foundedBeforeYear As Integer = years(rnd.Next(0, years.Length))
      Console.WriteLine("Teams founded before {0}:", foundedBeforeYear)
      For Each team in teams.FindAll( Function(x) x.Founded <= foundedBeforeYear )
         Console.WriteLine("{0}: {1}", team.Name, team.Founded)
      Next   
   End Sub
End Module
' The example displays output similar to the following:
'       Teams founded before 1930:
'       Detroit Red Wings: 1926
'       Chicago Blackhawks: 1926
'       Montreal Canadiens: 1909

Extension Methods

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

지정 된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.Gets an object that represents the method represented by the specified delegate.

적용 대상

추가 정보