Generic delegation in c # (@ WhiteTaken ),

Source: Internet
Author: User

Generic delegation in c # (@ WhiteTaken ),

Today I want to learn about generic delegation in c.

1. The general delegate, delegate, can pass in the parameter (<= 32), the declared method is public delegate void SomethingDelegate (int );

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace delegateSummary {8 public delegate void GetIntDelegate (int a); // declare a delegate 9 public class getIntClass {10 public static void SetDelegateString (int a, GetIntDelegate getIntDelegate) {// use delegate 11 getIntDelegate (a); 12} 13 public void getInt1 (int a) {// method 114 Console. writeLine ("getInt1 method call, parameter:" + a); 15} 16 public void getInt2 (int a) {// method 217 Console. writeLine ("getInt2 method call, parameter:" + a); 18} 19} 20 class Program {21 static void Main (string [] args) {22 getIntClass gc = new getIntClass (); 23 getIntClass. setDelegateString (5, gc. getInt1); // method 1, 2 as the delegate parameter 24 getIntClass. setDelegateString (10, gc. getInt2); 25 Console. writeLine ("=================="); 26 GetIntDelegate getIntDelegate; 27 getIntDelegate = gc. getInt1; // bind method 1 and 2 to the delegate 28 getIntDelegate + = gc. getInt2; 29 getIntClass. setDelegateString (100, getIntDelegate); 30 Console. read (); 31} 32} 33}

For the output result, note that the two methods are different. The first method is used as the delegate parameter, and the second method is bound to the delegate.

2. The Action of the generic delegate. A maximum of 16 parameters can be input and no return value is returned.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace delegateSummary {8 class Program {9 static void Main (string [] args) {10 TestAction <string> (getString, "WhiteTaken "); // Input Method 11 TestAction <int> (getInt, 666); 12 TestAction <int, string> (getStringAndInt, 666, "WhiteTaken"); 13 Console. read (); 14} 15 public static void TestAction <T> (Action <T> action, T p1) {// input a parameter to test 16 Action (p1 ); 17} 18 public static void TestAction <T, P> (Action <T, P> action, T p1, P p2) {// input two parameters for test 19 Action (p1, p2); 20} 21 public static void getString (string a) {// print 22 Console for int type. writeLine ("test Action, input string, and input parameter:" + a); 23} 24 25 public static void getInt (int) {// print the String type 26 Console. writeLine ("test Action, input int, and input parameter:" + a); 27} 28 29 public static void getStringAndInt (int a, string name) {// print 30 Console for int + string type. writeLine ("test Action, pass in two parameters, and the input parameter is:" + a + ":" + name); 31} 32} 33}

Test results:

3. For generic delegation Func, a maximum of 16 parameters can be input, with return values. (The statement is similar to Action, but the returned value is more)

4. predicate (not commonly used) of generic delegation. The returned value is bool, which is used to search for elements in Array and list. (Not used, and updated after use)

 

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.