How to Understand delegation and events and delegation events

Source: Internet
Author: User

How to Understand delegation and events and delegation events
How to Understand delegation and events

C # practitioners can only get started with C # if they understand the secrets of delegation and events. In my opinion, only by understanding what is delegated and how to create events using the delegated chain can we formally enter the world of C. Otherwise, you are just a programmer of C language.

1. What is delegation?

The essence of delegation is a function pointer, but in C #, in line with the principle that everything is an object, the delegate is a class (so it can be written in the namespace ), the IL Code shows that the delegate has its own constructor and member function.

The use of delegation in a program is usually divided into three parts: defining a delegate, defining a delegate is equivalent to writing a tag for the delegate; instantiating the delegate object is equivalent to adding a tag to an object, at the same time, the instance itself also complies with the label definition. The method processor, In the instantiated delegate object, transmits the method name (pointer) as the construction parameter to the object. At this time, the method processor must perform step encoding.

Delegation has three major functions in the program:

2. What is an event?

To better understand the delegation, we should understand what events are. In C #, events triggered by controls can be seen everywhere. Most new users do not know what the nature of events is or how they define an event. The event is essentially an instance object of a multi-channel broadcast delegate chain. Here, we emphasize that an event is an object that has been instantiated (so it can only be written in the class ). Although an event is essentially a delegate, it is also essentially different from a delegate.

In the program, we define an event by ourselves, which is usually divided into three parts, defining the delegate. Here we can also use the predefined delegate type EventHandler. The difference between the two is that custom delegate, the label (that is, the return value and function parameter) of the delegate is written by yourself. The EvenHandler delegate has no return value by default, and two parameters sender and EventArgs are passed in. The event is published, after the delegate is defined, it is necessary to publish the delegate tag to define it as an event. The event form is the same as the delegate form (which essentially instantiate the delegate object); subscribe to the event, you can add function methods to the instantiated delegate object (event) to complete the subscription.

3. Differences between multi-channel broadcast delegation chain and events

After reading so much, I think everyone understands the relationship between delegation and events. But what is the difference between multi-channel broadcast delegation chain and events? One of Microsoft's favorite tasks on the. NET platform is to constantly optimize the underlying code of the program so that programmers can focus more on the business logic of the program. In fact, the essence of an event is a structured encapsulation of the multi-channel broadcast delegate chain.

There are two differences between multi-channel broadcast delegation chain and events:

  • A multi-channel broadcast delegate chain needs to be instantiated using a delegate. After an event is defined, the so-called instantiation is not required (in essence, the delegate object is instantiated ).
  • When adding a new delegate, You need to instantiate a delegate and then add it. After the event is defined, subscribe to the event, you only need to add function methods (in essence, adding an instantiated delegate ).

 

I also want to have a deeper and more direct understanding of the Delegate and event. I can define the delegate and event, view the IL code, and analyze the essence of its operation.

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.