Delegation and events

Source: Internet
Author: User

Delegation and events are a very important part of C #. First, let's take a look at the definitions of these two concepts:

The delegate keyword is used to declare a reference type, which can be used to encapsulate naming or anonymous methods. The delegate is similar to the function pointer in C ++. However, the delegate type is safe and reliable. -- Msdn

Event Events are special types of multicast delegates that can only be called from their classes or structures (Publishers. If other classes or structures subscribe to this event, when the issuer class raises this event, it will call its event
Program Method. Events can be marked as public, private, protected, internal, or protectedinternal. These access modifiers define the way users access events of the class. -- Msdn

It is estimated that we can really understand these two paragraphs as a great guy. I am a newbie and can only "Oh, so, I still don't understand it... "Recently, cainiao D used delegation in the project. Just as Daniel passed by, ask him to help diagnose it.

The code is simplified as follows:

1 namespace ABC {2 3 Public Delegate void xxxdelegate (); // delegate declaration 4 5 Class ABC 6 {7 public xxxdelegate xxx; 8 9 Public void () 10 {11} 12 13 public void B () 14 {15} 16 17 Public void C () 18 {19} 20 21 public void main () 22 {23... 24 Pd (); 25 XX (); 26} 27 28 public void Pd () 29 {30 if () 31 {32 xxx = A; 33} 34 else if () 35 {36 xxx = B; 37} 38 else39 {40 xxx = C; 41} 42} 43 44 public void XX () 45 {46... 47 XXX (); 48} 49} 50}

After reading the code, Daniel frowned. "This is not the case with delegation ..."

"Delegate is not a safe function pointer. Can I use methods as variables ?" Cainiao D is puzzled.

"This understanding of delegation is good, but its application scenario is incorrect. These are all our own methods. Why don't we use a judgment to encapsulate the judgment logic into methods and directly call the methods? In this way, is your Commission useless ?" Daniel is very patient. "Commission, Commission, as the name suggests, should be executed by others ."

Cainiao D has nothing to say, because such writing is only using the "Pointer" idea, but the intent of delegation is to be put aside. "So, what are the application scenarios of delegation? ".

"Delegation is usually used when processing asynchronous threads, and it is also the basis of the event. A delegate defines a type. events can be seen as a variable of this type. You must have seen it during decompilation, events are decompiled into a private delegate and two methods, namely + = and-=, are used externally. Therefore, the application scenario is obvious. When an event is used, it is called by an asynchronous thread. I suggest you look at the subscription and publishing modes in the design mode ."

Cainiao D looked for the information and found that the information was messy. At this moment, Daniel gave a very simple example. He pointed to the USB port of the computer, "I don't know what devices will be inserted here, but I have defined a specification that can be inserted if it meets the specification. The same is true for events. If a button is used, I don't know how it will work. I will encapsulate a click event for it (button). You only need to register this event externally, you can make it have the desired effect when you click it. The same is true for spring dependency injection, which is often injected through interfaces. This is implemented through the 'interface' or 'interface' of the event, and the event is delegated internally, it is actually such a relationship."

Cainiao dzi thought about it and combined with the original experience-in the modal dialog box or the main form and subform (not the parent and child forms of the inheritance relationship, but created by the main form) A delegate occurs during inter-communication.
Needle "is indeed a joke.

Conclusion: Use Cases of delegation: asynchronous processes and events.
Subscriber mode: if not, see: http://blog.csdn.net/tjvictor/article/details/5223309
I am sorry for the dependency injection idea of spring to the next article.

Cainiao D hopes this talk will be helpful to you.

Delegation and events

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.