C # commonly used built-in system Delegation

Source: Internet
Author: User

C # commonly used built-in system Delegation
In the Common Language Runtime (CLR) environment, the system has built some common delegation for us, including the delegate of the Action class, the delegate of the Func class, the delegate of the Predicate <T>, and the delegate of the Comparison <T> class. The namespace of these delegates is System and the Assembly is mscorlib. dll. Today I will talk about how to use these delegates. Just as we have defined, to implement some functions, we can directly use the built-in system delegate to instantiate them, instead of explicitly defining a new delegate and assigning the naming method to the delegate. For example, copy the public static void Test () {Console. writeLine ("Just For Test");} static void Main (string [] args) {Action a = new Action (Test); // instantiate an Action delegate directly, you don't have to define the new delegate a ();} as long as you know what the system's built-in delegate is, what parameters are passed, and what values are returned, you can call it yourself like the above example, I will not go into details. The following is my summary of these four types of delegation, along with examples that combine anonymous methods and Lambda expressions. The results of the two methods are the same. 1. Action-class delegation 1. action delegate encapsulates a method. This method does not have parameters and does not return values. 2. action <T> the delegate encapsulates a method. This method has only one parameter and does not return a value. 3. action <T1, T2> delegates to encapsulate a method that has two parameters and does not return values ...... ...... 17. action <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> encapsulate a method, this method has 16 parameters and does not return values. The following uses the Action <T> delegate as an example to demonstrate how to use the delegate of the Action class. The delegate of this class is only different in the number of parameters. Copy the code static void Main (string [] args) {# region Action <T> delegate example // requirement: print the list of elements in the List of integer sets <int> list = new List <int> () {1, 2, 3, 4, 5 }; // assign the anonymous method to Action <T> delegate instance Action <int> concat1 = delegate (int I) {Console. writeLine (I) ;}; list. forEach (concat1); // assign the lambda expression to Action <T> delegate instance Action <int> concat2 = (I => Console. writeLine (I); list. forEach (concat2); Console. readKey (); # endregion} copy the code summary: A delegate can contain at least 0 parameters and a maximum of 16 parameters. The parameter types are invert and do not return values. Ii. Func class delegation 1. the Func (TResult) delegate encapsulates a method that does not have parameters but returns the type value specified by the TResult parameter 2. func (T, TResult) delegate encapsulate a method with a parameter and return the type value specified by the TResult parameter 3. func (T1, T2, TResult) delegate encapsulate a method with two parameters and return the type value specified by the TResult parameter ...... ...... 17. func <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult> delegate encapsulate a method. The method has 16 parameters and returns the value of the type specified by the TResult parameter. The following uses the Func <T, TResult> delegate as an example, demonstrate how to use the Func class delegate, which is only different in the number of parameters. Copy the code static void Main (string [] args) {# region Func <T, TResult> delegate example // requirement: search for a new set composed of all elements greater than 3 in the list of integer sets, and print the List <int> list = new List <int> () {1, 2, 3, 4, 5}; // allocate anonymous methods to Func <T, TResult> delegate instance Func <int, bool> concat1 = delegate (int I) {return I> 3 ;}; var newlist1 = list. where (concat1 ). toList (); // assign the Lambda expression to Func <T, TResult> delegate instance Func <int, bool> concat2 = I => I> 3; var newlist2 = list. where (c Oncat2 ). toList (); newlist1.ForEach (I => Console. writeLine (I. toString (); newlist2.ForEach (I => Console. writeLine (I. toString (); Console. readKey (); # endregion}: the Func class delegate can input at least one generic parameter (in, inverter). Up to one generic parameter (in, inverter) 16. There is only one input output generic parameter (out, covariant). This type is the return value type of the method encapsulated by this delegate. 3. the Predicate <T> delegate means to define a set of conditions and determine whether the specified object meets these conditions. An example of the Predicate <T> delegate is given below: copy the code static void Main (string [] args) {# region Predicate <T> delegate example // requirement: Find a new set composed of all elements larger than 3 in the list of integer sets, and print the set element List <int> list = new List <int> () {1, 2, 3, 4, 5 }; // assign the anonymous method to the Predicate <T> delegate instance Predicate <int> concat1 = delegate (int I) {return I> 3 ;}; var newlist1 = list. findAll (concat1); // assign the lambda expression to Predicate <T> delegate instance Predicate <int> Concat2 = (c => c> 3); var newlist2 = list. findAll (concat2); newlist1.ForEach (I => Console. writeLine (I); newlist2.ForEach (I => Console. writeLine (I); Console. readKey (); # endregion} copy code Summary: Predicate <T> delegates encapsulate a method that passes in a type parameter, which refers to the type of the object to be compared, this type of parameter is an inverter and receives a parameter (this parameter is an object to be compared according to the conditions defined in the method represented by this delegate, the parameter type is the type of the input type parameter), this method always returns the bool type value. True if the object meets the conditions defined in the methods represented by this delegate; otherwise, false. 4. Comparison <T> delegate represents the method for comparing two objects of the same type. The following is an example of Comparison <T> delegate: copy the code static void Main (string [] args) {# region Comparison <T> delegate example // requirement: print out all elements in the list of integer sets in reverse order. List <int> list = new List <int> () {1, 2, 3, 4, 5}; // assign the anonymous method to Comparison <T> delegate instance Comparison <int> concat1 = delegate (int I, int j) {return j-I ;}; // assign the lambda expression to Comparison <T> delegate instance Comparison <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}

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.