C # detailed delegation (2): implementation methods

Source: Internet
Author: User

Main applicability. 1. General replication code private delegate String getAString (); static void Main (String [] args) {int temp = 40; getAString stringMethod = new getAString (temp. toString); Console. writeLine ("String is {0}", stringMethod (); // stringMethod () is equivalent to calling temp. toString (); Console. readLine () ;}copy the Code. In this code, a delegate of the GetAString type is instantiated and initialized to reference the ToString () method of the integer variable temp. In C #, the delegate always accepts the constructor of a parameter in syntax. this parameter is the delegate reference method. In the preceding example, stringMethod () is equivalent to temp. toString () is identical to the Invoke () method that calls the delegate class. In fact, as shown in the red section of the IL code, the C # compiler uses the stringMethod. invoke () replaces stringMethod (). 032104 for easy input, C # supports transferring only the address name to the delegated instance (delegated inference). The following two lines of code are the same in the compiler. GetAString stringMethod = new getAString (temp. toString); getAString stringMethod = temp. toString; in fact, the delegated instance can reference the instance method or static method on any type of object, as long as the method signature matches the delegate signature. So the struct method can be passed to the delegate. 2. the multicast delegated multicast delegate has a linked delegation list called the call list. when calling the delegated instance, it will be called synchronously in the delegated order in the list. If the delegate has a return value, the return value of the last method in the list is used as the return value of the entire delegate call. Therefore, multicast delegation usually has the void return type. You can use + = to direct the delegate to the addresses of multiple methods, however, you must use + = to add a new method address after the delegate is instantiated (adding duplicate method addresses does not report errors, but does not execute them repeatedly ), if you want to remove the method address, you can use-= (you must retain at least one, that is, the removal of the last method address does not work ). In the following code, no matter which line is reserved separately, the final execution result is the same. GetAString stringMethod = new getAString (temp. toString); stringMethod + = temp. toString; stringMethod-= temp. toString; 3. delegate array copy code delegate double Operations (double x); class Program {static void Main () {Operations [] operations = {MathOperations. multiplyByTwo, MathOperations. square}; for (int I = 0; I <operations. length; I ++) {Console. writeLine ("Using operations [{0}]:", I); DisplayNumber (operations Ations [I], 2.0); DisplayNumber (operations [I], 7.94); Console. readLine () ;}} static void DisplayNumber (Operations action, double value) {double result = action (value); Console. writeLine ("Input Value is {0}, result of operation is {1}", value, result) ;}} struct MathOperations {public static double MultiplyByTwo (double value) {return value * 2;} public static double Square (double value) {ret Urn value * value ;}} the code above instantiates a delegate Array operations (same as the processing class instance). The elements of this array are initialized to different operations of the MathOperations class, traverse this array and apply each operation to two different values. The advantage of this usage is that different methods can be called in a loop. (It's not too early. I will write this article today, And the next article will write the implementation method of delegation .)

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.