It represents a defined set of conditions and method determines whether the specified object meet these conditions. This delegate is used by several methods and Array List class, for searching within the collection.
Consider the following definition of it:
// Represents the method that defines a set of criteria and determines whether
// the specified object meets those criteria.
//
// Parameters:
// obj:
// The object to compare against the criteria defined within the method represented
// by this delegate.
//
// Type parameters:
// T:
// The type of the object to compare.
//
// Returns:
// true if obj meets the criteria defined within the method represented by this
// delegate; otherwise, false.
public delegate bool Predicate < T > (T obj);
Type Parameter Description:
T: the type of objects to compare.
obj: Object to be compared according to the condition represented by the method thus defined delegate.
Return Value: If obj coincidence method thus delegate representation defined conditions, compared to true ; otherwise false .
Look at the code below:
{
List < String > listString = new List < String > ()
{
" One " , " Two " , " Three " , " Four " , " Fice " , " Six " , " Seven " , " Eight " , " Nine " , " Ten "
};
String[] arrayString = new String[]
{
" One " , " Two " , " Three " , " Four " , " Fice " , " Six " , " Seven " , " Eight " , " Nine " , " Ten "
};
public String[] GetFirstStringFromArray()
{
return Array.FindAll(arrayString, (c) => { return c.Length <= 3 ; });
}
public List < String > GetFirstStringFromList()
{
return listString.FindAll((c) => { return c.Length <= 3 ; });
}
public String[] GetFirstStringFromArray_1()
{
return Array.FindAll(arrayString, GetString);
}
public List < String > GetFirstStringFromList_1()
{
return listString.FindAll(GetString);
}
private bool GetString(String str)
{
if (str.Length <= 3 )
return true ;
else
return false ;
}
}
(1) First, the above two generic List and an array as a set of presentation objects, and build a collection.
(2) Next, both the respective FindALL all methods, see the following definitions:
Array : public T[] FindAll<T>(T[] array, Predicate<T> match);
List:public List<T> FindAll(Predicate<T> match);
Note that the two are used in the Predicate FindAll (generic delegate) as the type of parameter.
(3) Next, a way to show the use of both of Predicate:
The first: (c) => {return c.Length <= 3;};
The second: GetString (String str).
Both syntactically distinct, but actually do the same thing, first is to use Lambda expressions to build statements
Add that you can also write:
the delegate (String C) {return c.length <=. 3;} parameter defined as Predicate
Complete code:
This should be called the anonymous proxy.
There are other uses of the Predicate
Array.Find , Array.FindAll , Array.Exists , Array.FindLast , Array.FindIndex .....
List<T>.Find , List<T>.FindAll , List<T>.Exists , List<T>.FindLast , List<T>.FindIndex .....
extend:
In addition to the above mentioned, you can use the new method Predicate definition, to strengthen their code.
{
List < String > listString = new List < String > ()
{
" One " , " Two " , " Three " , " Four " , " Fice " , " Six " , " Seven " , " Eight " , " Nine " , " Ten "
};
public String GetStringList(Predicate < String > p)
{
foreach ( string item in listString)
{
if (p(item))
return item;
}
return null ;
}
public bool ExistString()
{
string str = GetStringList((c) => { return c.Length <= 3 && c.Contains( ' S ' ); });
if (str == null )
return false ;
else
return true ;
}
}
Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/08/29/2662925.html