Delegate for C #

Source: Internet
Author: User

There are one hours to go home for the New year, I wish you good luck in 2018, Big fortune.

C # Delegates look like the OC agent and Java interface, but it is very different, so-called Proxy and interface is independent of some of the method declaration (not implemented), as long as a class A inherit the proxy, you can implement them, then other classes can let a as their properties, Let a instead of these classes to do things, just call the time and timing by these classes to do, really to achieve to give a. Like you rob a ticket, to third-party software or cattle, third-party software and cattle is a, they rob the way and channel is the interface and agent, you do not need to care how to rob, you just pay or start to rob the ticket button.

Now let's look at some C # delegates, with three points in the difference:

Paste the code first to see how a C # delegate is implemented:

Using System;Delegate Int Numberchanger(IntN);//Declaration of DelegationNamespace Delegateappl{ Class Testdelegate { Static IntNum= 10; Public Static Int Addnum(IntP) {Num+=P; ReturnNum; } Public Static Int Multnum(IntQ) {Num*=Q; ReturnNum; } Public Static IntGetnum() { ReturnNum; } Static void Main(String[]Args) { Create a delegate instance NumberchangerNc1= New Numberchanger(Addnum); NumberchangerNc2= New numberchanger (multnum  //using delegate object Call method  Nc1 (1< Span class= "pun");  console. Writeline ( "Value of Num: {0}" , Getnum ());  Nc2 (1console. Writeline ( "Value of Num: {0}" , Getnum ()); 
/span>
Numberchanger NC;
NC = nc1 + nc2;
NC (5);//delegate multicast invocation (sequential execution)
     Console.  WriteLine("Value of Num: {0}", getnum());  Console.  ReadKey();          
   } }} 
Printed as follows:
Valuenum:onevaluenum:       One
ValueNum:90
The 1th difference: the delegate declaration of C # restricts the type of method (the parameter to unify) to represent a class of methods, the method name is arbitrary, so there is the function of multicast, it is clear that the implementation of multiple methods stitching together sequentially, and OC agent does not restrict the type of methods, but to determine which method name.

The 2nd difference: a delegate instance of C # is a method variable, as long as the type is consistent. An OC agent needs a class to inherit, which is then given to this class for implementation. C # is a bit more flexible.

The 3rd difference: the time to get off the bus is not much, next time to fill up. Happy new year!
             

Delegate for C #

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.