Common in C # system built-in delegate usage detailed (transcription)

Source: Internet
Author: User

Common system built-in delegates in C #

Action class, Func class, predicate<t>, Comparison<t> delegate

    • Delegate for Action class
  1. The action delegate encapsulates a method that does not have parameters and does not return a value
  2. The Action<t> delegate encapsulates a method that has only one parameter and does not return a value

  3. The Action<t1,t2> delegate encapsulates a method that has two parameters and does not return a value
  4. Static voidMain (string[] args) {#regionAction<t> Delegate Example//Requirements: Print out the elements of the integer collection listlist<int> list =Newlist<int> () {1,2,3,4,5 }; //assigning an anonymous method to a action<t> delegate instanceaction<int> CONCAT1 =Delegate(inti) {Console.WriteLine (i);}; List.  ForEach (CONCAT1); //Assigning a lambda expression to an action<t> delegate instanceaction<int> concat2 = (i =Console.WriteLine (i)); List.  ForEach (CONCAT2);  Console.readkey (); #endregion }
    • Delegate for the Func class
  1. 1.Func (TResult) delegate encapsulates a method that encapsulates a type value that does not have a parameter but returns the TResult parameter
  2. The Func (T,tresult) delegate encapsulates a method that has a parameter and returns the type value specified by the TResult parameter
  3. The Func (T1,t2,tresult) delegate encapsulates a method that has two parameters and returns the type value specified by the TResult parameter
  4. Static voidMain (string[] args) {  #regionFunc<t,tresult> Delegate Example//requirement: Finds a new collection of all elements greater than 3 in the list of integral types and prints out the collection elementslist<int> list =Newlist<int> () {1,2,3,4,5 }; //assigning an anonymous method to a func<t,tresult> delegate instancefunc<int,BOOL> CONCAT1 =Delegate(inti) {returni >3; }; varNewlist1 =list. Where (CONCAT1).  ToList (); //Assigning a LAMBDA expression to an func<t,tresult> delegate instancefunc<int,BOOL> concat2 = i = i >3; varNewlist2 =list. Where (CONCAT2).  ToList (); Newlist1. ForEach (i=Console.WriteLine (i.ToString ())); Newlist2. ForEach (i=Console.WriteLine (i.ToString ()));  Console.readkey (); #endregion}
    • Predicate<t> delegate

Represents a method that defines a set of conditions and determines whether the specified object conforms to these conditions

  1. Static voidMain (string[] args) {  #regionPredicate<t> Delegate Example//requirement: Finds a new collection of all elements greater than 3 in the list of integral types and prints out the collection elementslist<int> list =Newlist<int> () {1,2,3,4,5 }; //assigning an anonymous method to a predicate<t> delegate instancepredicate<int> CONCAT1 =Delegate(inti) {returni >3; }; varNewlist1 =list.  FindAll (CONCAT1); //Assigning a lambda expression to an predicate<t> delegate instancepredicate<int> CONCAT2 = (c = c >3); varNewlist2 =list.  FindAll (CONCAT2); Newlist1. ForEach (i=Console.WriteLine (i)); Newlist2. ForEach (i=Console.WriteLine (i));  Console.readkey (); #endregion}
    • Comparison<t> delegate

Represents a method that compares two objects of the same type

  • Static voidMain (string[] args) {  #regionComparison<t> Delegate Example//Requirement: Prints all elements in the list of integral types in reverse orderlist<int> list =Newlist<int> () {1,2,3,4,5 }; //assigning an anonymous method to a comparison<t> delegate instancecomparison<int> CONCAT1 =Delegate(intIintj) {returnJ-i;}; //Assigning a lambda expression to an comparison<t> delegate instancecomparison<int> CONCAT2 = (i, j) + J-i; List.  Sort (CONCAT1); List. ForEach (c=Console.WriteLine (C.tostring ())); List.  Sort (CONCAT2); List. ForEach (c=Console.WriteLine (C.tostring ()));  Console.readkey (); #endregion}

Common in C # system built-in delegate usage detailed (transcription)

Related Article

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.