. Net C # Delegate personal opinions

Source: Internet
Author: User

Today, I suddenly thought that I forgot my understanding of delegation and I would like to review it again. I hope it will help you.

First, let's take a look at msdn's explanation of delegation.:

A delegate is a type that defines the method signature. When instantiating a delegate, You can associate its instance with any method with a compatible signature. You can call a method by entrusting an instance.

What is Delegation
First, you need to know what a delegate is. In terms of plain understanding, you can regard the delegate as a thing used to execute methods (functions.

 

How to Use Delegation
When using a delegate, you can treat it like a class. That is, declare the object first, and then instantiate the object. The class is called an object or an instance after instantiation, but the delegate is still called a delegate after instantiation.

The following is my understanding code for delegation:

 

1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 6 applications delegated by namespace 7 {8 Public Delegate void wenhou (string name); // define a delegate wenhou 9 class program10 {11 Private Static void englishwenhou (string name) // define an English greeting output Goodmorning + name 12 {13 console. writeline ("Goodmorning {0}", name); 14} 15 Private Static void chineswenhou (string name) // define a Chinese greeting output good morning + name 16 {17 console. writeline ("Good Morning {0}", name); 18} 19 20 private static void delegatewenhou (string name, wenhou Wh) // defines a variable that delegates the wenhou type, this delegate will call the method based on the method name and pass the parameter to the corresponding Method 21 {22 wh (name); 23} 24 static void main (string [] ARGs) 25 {26/* The first way is to use the delegate method (this method is to pass the value to the delegate in the form of parameters) 27 28 29 delegatewenhou ("Cao Jun", englishwenhou ); // set as the delegate for Chinese greetings 30 31 console. writeline ("--------------------------------- \ n"); 32 33 delegatewenhou ("Jun. CA O ", chineswenhou); // set to delegate for English greetings 34 35 console. readkey (); // obtain the Input key -- used again not to immediately close the debugging window 36 */37 38/* 39 input: 40 Goodmorning Jun. cao41 ------------------------------- 42 43 Good morning, Cao Jun 44 */45 46 // method 2 use the delegate method (directly send the delegate value) 47 48 49 50 wenhou wh = new wenhou (engishwenhou ); // Add an English greeting Method to the delegate ^-^ is it the same as the instantiated object and the instantiated class? In fact, the delegate is a class when the compiler executes it, but the delegate is type safe... 51 52 wh ("Jun. cao "); // greet 53 console in English through Delegate call. writeline ("------------------------------- \ n"); 54 Wh + = new wenhou (chineswenhou); // Method for adding Chinese greetings to the delegate 55 56 wh ("Cao Jun "); // call the delegate. The delegate calls the method bound to the delegate in sequence 57 console. writeline ("------------------------------- \ n"); 58 59 wh-= new wenhou (englishwenhou); // remove methods from the delegate. ^-^ You can add a delegate method to remove the delegate method. + = Add a method to the delegate.-= remove the existing method 60 61 Wh ("Cao Jun") in the delegate "); 62 63 console. readkey (); 64/* 65 input: 66 goodmorningjun. cao67 --------------------------------- 68 69 Goodmorning Cao Jun 70 good morning Cao Jun 71 ----------------------------------- 72 73 Good morning Cao Jun 74 75 76 */77} 78} 79}

 

 

 

 

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.