[Switch] C # Delegate action, action <t>, func <t>, predicate <t>

Source: Internet
Author: User

In the CLR environment, several commonly used delegate actions, actions, func, and predicate are provided. Generally, when delegate is used, try not to define another delegate by yourself. The built-in system can meet most of the requirements and make the Code conform to the specifications.

1. The method encapsulated by actionaction has no parameters and no return value. The declared prototype is: 1 Public Delegate void action (). Usage: Copy code 1 Public void alert () 2 {3 console. writeline ("this is a warning"); 4} 5 6 action T = new action (alert); // instantiate an action delegate 7 T (); copy the Code. If the statements in the delegate method are short, you can also use the lambd expression to define the method in the delegate, as shown in the following code: 1 Action t = () => {console. writeline ("this is a warning") ;}; 2 T (); 2. Action <t> action <t> is a generic implementation of action and no return value, however, up to 16 parameters can be input. The declared prototype of the two parameters is: 1 Public Delegate void action <in T1, in T2> (T1 arg1, T2 arg2). The usage is as follows: copy code 1 private void showresult (int A, int B) 2 {3 console. writeline (a + B); 4} 5 6 action <int, int> T = new action <int, int> (showresult ); // The delegate with two parameters but no return value is 7 T (2, 3). You can also copy the code and directly define the method in the delegate using the lambd expression. The Code is as follows: 1 Action <int, int> T = (a, B) => {console. writeline (a + B) ;}; 2 T (2, 3); 3. The func <t> delegate always has a return value, the type of the return value is the last one in the parameter. You can either input a parameter or a maximum of 16 parameters, but you can enter a maximum of 16 parameters. The declaration prototype of the return value of two parameters is: 1 Public Delegate tresult func <in T1, in T2, out tresult> (T1 arg1, T2 arg2); usage: copy the Code 1 Public int compare (int A, int B) 2 {3 return A> B; 4} 5 6 func <int, Int, bool> T = new func <int, Int, bool> (compare ); // input two int parameters and return the bool value 7 bool result = T (2, 3). Copy the code and use the lambd expression to directly define the method in the delegate, the Code is as follows: 1 func <int, Int, bool> T = (a, B) =>{ return A> B ;}; 2 bool result = T (2, 3 ); 4. the predicate <t> delegate defines a set of conditions and determines whether the specified object meets these conditions. The return value is always of the bool type and the declared prototype is: 1 Public Delegate bool predicate <in T> (t obj); usage: Copy code 1 Public bool match (INT Val) 2 {3 return Val> 60; 4} 5 6 predicate <int> T = new predicate <int> (MATCH); // defines a comparison delegate 7 int [] arr = {13, 45, 26, 98, 3, 56, 72, 24}; 8 int first = array. find (ARR, T); // find the first element in the array larger than 60 and copy the code. You can also use the lambd expression to directly define the method in the delegate. The Code is as follows: 1 predicate <int> T = Val =>{ return Val> 60 ;}; // defines a comparison delegate 2 int [] arr = {13, 45, 26, 98, 3, 56, 72, 24}; 3 int first = array. find (ARR, T); // find the first element in the array greater than 60. Conclusion: if the method to be delegated has no parameters and no return values, you can think of action with parameters but no return values. <t> if no parameter has a return value, and a parameter has a return value, you can think of func <t>. return values of the bool type, this method is usually used in comparator. To delegate this method, you can use predicate <t>

[Switch] C # Delegate action, action <t>, func <t>, predicate <t>

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.