[Post] predicate usage

Source: Internet
Author: User
Predicate generic delegation

Defines a set of conditions and determines whether the specified object meets these conditions. This delegate is used by several methods of the array and List classes to search for elements in the collection.

Let's 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 parameters:


T:
Type of the object to be compared.

 
OBJ:

Objects to be compared according to the conditions defined in the methods represented by this delegate.

Return Value: If
OBJIf the conditions defined in the methods represented by this delegate are metTrueOtherwise
False.

See the following code:

Public class genericdelegatedemo

{

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, we use the array and generic list sets as the demo objects and construct a set.

(2) then, the two use all their findall methods at the same time. See the following definition:

Array:
Public T [] findall <t> (T [] array,
Predicate <t> match );


List: public list <t>
Findall (predicate <t> match );


Note that both findall uses predicate (generic delegation) as the parameter type.

(3) Use both methods to demonstrate predicate usage:


First: (c) => {return C. Length
<= 3 ;};

Second:
Getstring (string Str ).

The two are obviously different in syntax, but they actually do the same thing. The first is a statement constructed using a Lambda expression.

In addition, you can write as follows:

Delegate (string c) {return
C. Length <= 3;} is a parameter defined by predicate.

Complete code:

XX. findall (delegate (string c) {return C. Length <= 3 ;});

This should be called an anonymous proxy.

Other 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 .....

Extension:

In addition to the above mentioned, you can use predicate to define new methods to enhance your code.

Public class genericdelegatedemo
{
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;

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.