What is a delegate first to know what a delegate is, in the most understandable words, you can think of a delegate as a thing to execute a method (function). How to use a delegate when you use a delegate, you can treat it as if you were a class. It is declared first, then instantiated. Just a little different, a class is called an object or an instance after it is instantiated, but the delegate is still called a delegate after it is instantiated. declarations, such as:1 namespaceVCZX.PROCSHARP.EXC2 {3 Delegate DoubleMathsop (Doublex);4 //class Defination here5this declares a delegate, meaning that any function that has a return value of double and only one parameter is a double can be invoked with this delegate. Note: The declaration position of the delegate is inside the namespace, outside of the class. In fact, the declaration of a delegate can also be inside a class, or even anywhere a class can be declared. Instantiation: First we must first have a method that satisfies the declaration of a delegate, assuming a method that returns a number twice times: 1classmathsoperations2{3 Public Static DoubleMultiplyBy2 (Doublevalue)4 {5 returnValue *2;6 }7with such a method, we can instantiate a delegate: Mathsop Operation=NewMathsop (MATHSOPERATIONS.MULTIPLYBY2); When instantiating a delegate, give it a parameter, which is the method of the delegate execution, which can be either a static method or an instance method (this is different from a function pointer, the function pointer can only invoke a static method), such as: Mathsop operation=NewMathsop (NewClass1 (). METHOD1); After instantiating a delegate, you can use this delegate to invoke the method:Doubleresult = Operation (1.23); example code: 1namespaceVCZX.PROCSHARP.EXC2{ 3 Delegate DoubleMathsop (Doublex); 4 classStart5 { 6 Public classMyDelegate7 { 8 Public Static DoubleMultiplyBy2 (Doublex)9 {Ten returnX *2; One } A } -[STAThread] - Static voidMain (string[] args) the { -Mathsop operation =NewMathsop (mydelegate.multiplyby2); - Doublex =1.23; - Doubleresult =operation (x); +Console.WriteLine ("{0} Multiply by 2 is {1}", x, result); -Console.read (); + } A } atthe delegate used before the multicast delegate contains only one method call. The number of times the delegate was invoked is the same as the number of calls. If you want to invoke more than one method, you need to display the delegate multiple times. In fact, a delegate can also contain multiple methods, such a delegate is a multicast delegate. The multicast delegate is derived from System.MulticastDelegate, and its combine method allows multiple method calls to be linked together, and we can+ = To add a calling method to a delegate, or to use the-=remove the calling method. Example: 1namespaceVCZX.PROCSHARP.EXC2{ 3 Public classMyDelegate4 { 5 Public Static voidMultiplyBy2 (Doublevalue)6 { 7 Doubleresult = value *2; 8Console.WriteLine ("multiplying by 2: {0} gives {1}", value, result); 9 }Ten One Public Static voidSqure (Doublevalue) A { - Doubleresult = value *value; -Console.WriteLine ("squaring: {0} gives {1}", value, result); the } - } - - Delegate voidMathsop (Doublex); + - classStart + { A[STAThread] at Static voidMain (string[] args) - { -Mathsop operation =NewMathsop (mydelegate.multiplyby2); -Operation + =NewMathsop (mydelegate.squre); - Doublex =1.23; -operation (x); in -Operation-=NewMathsop (mydelegate.multiplyby2); tooperation (x); + -Console.read (); the } * } $} output: Multiplying by2:1.23Gives2.46Squaring:1.23Gives1.5129Squaring:1.23Gives1.5129Note that the multicast delegate declaration must return void, otherwise the return value does not know where it should be sent back. In this case, I did a test: if the declaration of the delegate is not returned void, the return value returns the return value of the last method that is chained to the delegate chain, and compiles without errors. Why use delegates with delegates enables programmers to encapsulate a method reference within a delegate object. You can then pass the delegate object to the code that invokes the referenced method without knowing at compile time which method will be called. With C or CUnlike function pointers in + +, delegates are object-oriented and type-safe.
C # Delegate