Is Predicate <T> not available in .NET 3.0+
Is Predicate available anywhere in .NET? From MSDN http://msdn.microsoft.com/en-us/library/bfcke1bz.aspx I don't see the predicate anywhere. I see an anonymous one that returns boolean but not generics or the Predicate keyword.
source to share
Instead of a built-in delegate for a predicate, you can use it as a type and then pass it. Here's an example:
var list = new List<int>();
list.AddRange(Enumerable.Range(1, 10));
Predicate<int> p = delegate(int i)
{
return i < 5;
};
list.RemoveAll(p);
list.ForEach(i => Console.WriteLine(i));
EDIT: to declare a predicate using an existing method, you must use:
Predicate<int> p = IsLessThanFive;
public bool IsLessThanFive(int number)
{
return number < 5;
}
Alternatives that are more common would be as follows:
list.RemoveAll(delegate(int i) { return i < 5; });
// or...
list.RemoveAll(i => i < 5);
EDIT: To answer your other question in a comment on another post, a delegate is a type that defines a method signature and can be used to pass methods to other methods as arguments. If you are familiar with C ++, they are like function pointers. For more information on delegates check this MSDN page .
source to share
This page on MSDN is about the Predicate (T) delegate, which it states:
Represents a method that defines a set of criteria and determines if the specified object meets those criteria.
source to share