predicate
Generic delegate: Represents a method that defines a set of conditions and determines whether the specified object conforms to those conditions. This delegate is used by several methods of the Array and list classes to search for elements in the collection.
Type parameter Description:
T: The type of object to compare.
obj: the object to be compared by the criteria defined in the method represented by this delegate.
Return value: trueif obj conforms to the condition defined in the method represented by this delegate;
Look at the code below
public class Genericdelegatedemo
{
list<string> liststring = new List<string> ()
{
"One", "one", "three", "four", "Fice", "Six", "Seven", "Eight", "Nine", "Ten"
};
string[] arraystring = new string[]
{
"One", "one", "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, it takes an array and a generic list of two sets as the presentation object and constructs the collection.
(2) Next, both use each of their findall methods, as defined below:
Array:public t[] findall<t> (t[] array, predicate<t> match);
List:public list<t> FindAll (predicate<t> match);
Note that both findall use the predicate (generic delegate) as the type of the parameter.
(3) Next, use the two ways to show the use of predicate:
The first type: (c) = = {return c.length <= 3;} ;
Second type: GetString (String str).
The two are significantly different in syntax, but actually do the same thing, the first is a statement built using lambda expressions, and there is no detail about Lambda here, see author c#3.0 feature related articles.
Plus, you can write that, too.
Delegate (String c) {return c.length<=3;} as a parameter defined by predicate
Full code:
| Xx. FindAll (Delegate (String c) {return c.length <= 3;}) ; |
This should be called an anonymous proxy.
Other uses to predicate have
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 .....
Extension:
In addition to the above mentioned, you can use predicate to define new methods to strengthen your code.
public class Genericdelegatedemo
{
list<string> liststring = new List<string> ()
{
"One", "one", "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;
}
} |
Also solves the above problem, here wordy just to illustrate the use of predicate.
For the application of predicate of course, this is not something new, today, think carefully about the taste of the C # is really an elegant language.
For beginners Reference.
I would like to do some of the following generic delegates to do some introduction and summary, in the process of understanding predicate, found that as long as the understanding of generics, delegates and anonymous agents,
Of course you know that lambda expressions are better, and you can apply them flexibly at the right time. That is, just defining a different delegate,
One is that you can define such a delegate, and then use it;
Second, you need to know how a good delegate like predicate, Func, and action are defined. or use the appropriate access to the next MSDN.
Such as:
Func (): Encapsulates a method that does not have a parameter but returns the type value specified by the TResult parameter.
Func (T1, T2, TResult): Encapsulates a method that has two parameters and returns the type value specified by the TResult parameter, and no T2 is encapsulating a parameter and ...
Action () Action (T1) Action (T2): Encapsulates a method that specifies a number of parameters (such as () no arguments, (T1) a parameter, and so on) and does not return a value. This is similar to Func, but has no return value.
To remind you that:
x=>x+x;
And
x=> {return x+x;} is equivalent.
Generic delegate predicate/func/action for C #