Описание тега predicate
A Predicate is a method which represents a set of criteria and decides for a given object if these criteria are fulfilled or not. In computer languages a Predicate is expressed as a function which takes a single object as input parameter and returns a boolean value.
Example of a Predicate in.NET Framework / C#
In the.NET Framework the most general form of a Predicate is represented by a generic delegate:
public delegate bool Predicate<in T>(T obj)
An often used alternative representation is:
public delegate TResult Func<in T, out TResult>(T arg)
where TResult
is of type bool
.
An example of a concrete instance of such a delegate is a method which decides for a given Product
by a special citerion if it is expensive or not:
public bool IsExpensive(Product product)
{
return product.UnitPrice > 1000;
}
Predicates are often used for defining criteria to filter a subset of data out of a larger set and are often expressed as anonymous methods or by lambda expressions:
IEnumerable<Product> expensiveProducts =
allProducts.Where(p => p.UnitPrice > 1000);