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.
Take a look at its definition below:
Summary:
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 object to compare.
obj:The object to compare according to the criteria defined in the method represented by this delegate.
return value: IfobjConforms to the criteria defined in the method represented by this delegate;true; otherwisefalse。
Look at the following code:
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;}
Parameters as 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 .....
Extended:
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.
I did the next exercise with MVC for the explanation.
1. Structure
2, respectively, under the Controller to establish PredicateController.cs; Under Views, create the predicate folder, and then create an index (View) under it.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Namespace Predicatepractice.controllers
{
public class Predicatecontroller:controller
{
//
GET:/predicate/
Public ActionResult Index ()
{
list<string> strlist = new List<string> () {
"One", "one", "three", "four", "Fice", "Six", "Seven", "Eight", "Nine", "Ten"
};
string[] arrayList = new string[] {
"One", "one", "three", "four", "Fice", "Six", "Seven", "Eight", "Nine", "Ten"
};
Returns an array of words with a length less than 3 in ArrayList (string[])
string[] NewArray = Array.findall (arrayList, C = C.length < 4);
Returns an array of words in strlist that are less than 3 in length (list<string>)
list<string> NewList = Strlist.findall (c = c.length < 4);
Use the Checkstr method (generic delegate) to filter
string[] Newarrayusecheckfunc = Array.findall (arrayList, CHECKSTR);
Use the Checkstr method (generic delegate) to filter
list<string> newlistusecheckpredicate = Strlist.findall (CHECKSTR);
viewdata["NewArray"] = NewArray;
viewdata["newlist"] = NewList;
viewdata["Newarrayusecheckfunc"] = Newarrayusecheckfunc;
viewdata["newlistusecheckpredicate"] = newlistusecheckpredicate;
return View ();
}
<summary>
Checks if the string s length is less than 4
</summary>
<param name= "S" ></param>
<returns></returns>
private bool Checkstr (string S)
{
if (S.length < 4)
return true;
Else
return false;
}
}
}
<%@ page title= "" Language= "C #" masterpagefile= "~/views/shared/site.master" inherits= "System.Web.Mvc.ViewPage"% >
<asp:content id= "Content1" contentplaceholderid= "titlecontent" runat= "Server" >
Index
</asp:Content>
<asp:content id= "Content2" contentplaceholderid= "maincontent" runat= "Server" >
<br/>
Array {"One", "one", "three", "four", "Fice", "Six", "Seven", "Eight", "Nine", "Ten"}<br/>
--------------Begin--------------<br/>
<font color= "Red" ><b>string[] NewArray = Array.findall (arrayList, C = c.length < 3); </b></ Font><br/>
Results:
<% ilist<string> NewArray = (viewdata["NewArray"] as ienumerable<string>). Tolist<string> (); %>
<%= html.display ("<br/>")%>
<% for (int i = 0; i < Newarray.count; i++)%>
<% {%>
<%= Html.label (Newarray[i]. ToString ())%>
<%}%>
<br/>
------------------------------<br/>
<font color= "Red" ><b>List< string > NewList = Strlist.findall (c = c.length < 3); </b></ Font><br/>
Results:
<% ilist<string> NewList = (viewdata["NewList"] as ienumerable<string>). Tolist<string> (); %>
<% for (int j = 0; J < Newlist.count; J + +)%>
<% {%>
<%= Html.label (Newlist[j]. ToString ())%>
<%}%>
<br/>
------------------------------<br/>
<font color= "Red" ><b>lstring[] Newarrayusecheckfunc = Array.findall (arrayList, CHECKSTR);</b>< /font><br/>
Results:
<% ilist<string> Newarrayusecheckfunc = (viewdata["Newarrayusecheckfunc"] as ienumerable<string>). Tolist<string> (); %>
<% for (int k = 0; k < newarrayusecheckfunc.count; k++)%>
<% {%>
<%= Html.label (Newarrayusecheckfunc[k]. ToString ())%>
<%}%>
<br/>
------------------------------<br/>
<font color= "Red" ><b>List< string > newlistusecheckpredicate = Strlist.findall (CHECKSTR); </b ></font><br/>
Results:
<% ilist<string> newlistusecheckpredicate = (viewdata["Newlistusecheckpredicate"] as IEnumerable<string >). Tolist<string> (); %>
<% for (int l = 0; l < newlistusecheckpredicate.count; l++)%>
<% {%>
<%= Html.label (Newlistusecheckpredicate[l]. ToString ())%>
<%}%>
<br/>
---------------End---------------<br/>
</asp:Content>
Effect:
Generic Delegate Predicate/func/action