03. C # Delegate (Chapter 2 1.1 ),

Source: Internet
Author: User

03. C # Delegate (Chapter 2 1.1 ),

Chapter 1 1.5-1.8 describes com, dynamic types, and. NET platform. Every heart has its own standards. Listening to the words of a family makes people uncomfortable. I believe that I have my own standards and programming concepts, I also don't want to code so many descriptive texts. Skip it. Of course I have read it. Let's start with the question. This article is about delegation. (I am also studying C ++ while I have a deep understanding of C #. The delegation is indeed similar to the function pointer. You can also solve it .)

Four basic components of delegation:

Declare the delegate using the delegate keyword

1 public delegate void Drive(string name);

As shown above, a Delegate type Drive is declared, derived from System. MulticastDelegate, and System. MulticastDelegate inherits from System. Delegate. This type has its own methods and attributes. The signature of the method that matches this type must use a string type as the parameter and return a void type (that is, no return). It can be a static method or an instance method.

1 public class Car 2 {3 // instance method 4 public void Drive (string name) 5 {6 Console. WriteLine (name + "driving !!! "); 7} 8 9 // static method 10 public static void StaticDrive (string name) 11 {12 Console. WriteLine (name +" driving !!! "); 13} 14}

As shown above, the Drive method in the Car class is an instance method. before using this method, there must be a Car-type instance. StaticDrive is a static method and can be called using the class scope, the signatures of the preceding two methods meet the requirements of the preceding delegate for method signatures.

Next, create a Drive-type delegated instance and call it.

1 class Program 2 {3 static void Main (string [] args) 4 {5 6 // declare two delegated instances 7 Drive drive0, drive1; 8 Car car = new Car (); 9 10 // assign values to the delegated instance. One uses the static method and the other uses the 11 drive0 = new Drive (Car. staticDrive); 12 drive1 = new Drive (car. drive); 13 14 // call the delegate instance method 15 16 // use the Invoke method in the delegate instance to call 17 drive0.Invoke ("Leo "); 18 // use the shorthand method 19 drive1 ("DuDu"); 20 21 Console. readKey (); 22 23} 24}

The above method only calls a simple operation, and entrusts more cases to be used in combination with multiple operations. You can use Delegate. Combine and Delegate. Remove to increase or decrease the operation, which is generally abbreviated as + = and-=.

1 class Program 2 {3 static void Main (string [] args) 4 {5 6 // declare two delegated instances 7 Drive drive0, drive1; 8 Car car = new Car (); 9 10 // assign values to the delegated instance. One uses the static method and the other uses the 11 drive0 = new Drive (Car. staticDrive); 12 drive1 = new Drive (car. drive); 13 14 // call the delegate instance method 15 16 // use the Invoke method in the delegate instance to call 17 drive0.Invoke ("Leo "); 18 // use the shorthand method 19 drive1 ("DuDu"); 20 21 22 drive0 + = drive1; 23 drive0.Invoke ("Leo"); 24 25 Console. readKey (); 26 27} 28}

The running result is as follows:

The operations + = or-= are more convenient and easy to understand.

  

 

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.