Sample code sharing for generic delegates in C #

Source: Internet
Author: User
This article mainly describes generic delegates in C #. Have a good reference value, follow the small series together to see it

Learn about generic delegates in C # today.

1. General delegate, delegate, can also pass in parameters (<=32), declared method is public delegate void Somethingdelegate (int a);

using system;using system.collections.generic;using system.linq;using System.Text;using System.threading.tasks;namespace delegatesummary {public delegate void getintdelegate (int a);//Declare a delegate public class Geti Ntclass {public static void setdelegatestring (int a,getintdelegate getintdelegate) {//Use delegate getintdelegate (a);} public void GetInt1 (int a) {//Method 1 Console.WriteLine ("GetInt1 method invocation, Parameter:" + a), "public void GetInt2 (int a) {//Method 2 Console.writ Eline ("GetInt2 method invocation, Parameter:" + a);  }} class Program {static void Main (string[] args) {getintclass gc=new getintclass ();  Getintclass.setdelegatestring (5, gc.getint1);   Method Getintclass.setdelegatestring (Gc.getint2) as the parameter of the delegate;  Console.WriteLine ("=====================");  Getintdelegate getintdelegate;     Getintdelegate = Gc.getint1;  Bind the method to the delegate getintdelegate + = Gc.getint2;   Getintclass.setdelegatestring (getintdelegate); Console.read (); }  }}

Output results, note the difference between the two methods, the first method as a delegate parameter, the second is to bind the method to the delegate.

2. Generic delegate action, up to 16 parameters, no return value.

using system;using system.collections.generic;using system.linq;using System.Text;using System.threading.tasks;namespace Delegatesummary {class Program {static void Main (string[] args) {testaction<string > (getString, "Whitetaken");  Incoming method Testaction<int> (GETINT, 666);  Testaction<int, String> (Getstringandint, 666, "Whitetaken");   Console.read (); } public static void Testaction<t> (Action<t> action,t p1) {//action passed in a parameter test Action (p1);} public stat IC void Testaction<t, p> (action<t, p> action, T p1, P p2) {//action incoming two parameters test Action (P1,P2);} public static V OID getString (String a) {//implement int type print Console.WriteLine ("Test action, pass in string, and pass in Parameter:" +a);} public static void GetInt (int a) {//Implement string type Print Console.WriteLine ("Test action, pass in int, and pass in Parameters:" + a);} public static void Getstringandint (int A, string name) {//Implements Int+string type Print Console.WriteLine ("Tests the action, passes in two arguments, and the passed in parameter is : "+ A +": "+name);}}} 

Test results:

3. Generic delegate func, up to 16 parameters, with a return value. (similar to action, just a return value)

4. Generic delegate predicate (not very common), the return value is bool, used to search for elements in array and list. (not used, wait for the update)

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.