KNOW ABOUT GENERIC PREDICATE DELEGATE
Predicate
A predicate is a formal countenance that yields a termination of either genuine or false. Predicates are ofttimes utilised in contingent processing, specially with “if” statements. For example, in the mass C# cipher the asseverate is “value > 10″. If the continuance uncertain is large than decade the countenance returns true. If it is decade or less, the asseverate evaluates as false.
The Predicate<T> Delegate
The Predicate<T> assign is a pre-defined assign that was introduced in the .NET support 2.0. It is a generic assign that is utilised to meaning a method that accepts a constant of whatever identify and returns a mathematician result. The assign is utilised by whatever of the classes provided by the .NET framework. For example, you crapper wager the table of arrays and generic lists for items that foregather presented criteria. The criteria are provided to the pertinent methods using a Predicate<T> parameter.
To demonstrate, add the mass cipher to a housing covering and fulfil it in the debugger to wager the results. This warning creates an number clothing containing figure maturity numbers. It uses the FindAll method to create added clothing containing the maturity drawing between set and nine. The information is circumscribed in a method that is passed to the second, Predicate<T> argument.
static vacuum Main()
{
int[] primes = newborn int[] { 2, 3, 5, 7, 11, 13, 17, 19, 21 };
int[] smallPrimes = Array.FindAll(primes, IsSingleDigit); // { 2, 3, 5, 7 }
}
static bool IsSingleDigit(int value)
{
convey 0 <= continuance && continuance <= 9;
}
We crapper simplify the cipher using an nameless method for the asseverate parameter, as daylong as it has a azygos discussion and returns a Boolean. The cipher beneath is functionally same to the preceding sample.
int[] primes = newborn int[] { 2, 3, 5, 7, 11, 13, 17, 19, 21 };
int[] smallPrimes = Array.FindAll(primes, delegate(int value) { convey 0 <= continuance && continuance <= 9; })