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 #