C # event mechanism

Source: Internet
Author: User

In all the introduction to the C # event mechanism, I prefer the publisher/Subscriber (Publisher/subscriber) description. Understanding the event mechanism is not an easy task, and the thought involved is worthy of our good study.

This resource comes from the "C # and. NET Technology platform Practical walkthrough"--China Youth Press

When it comes to events, we involve two roles: the event publisher (publisher) and the event Subscriber (Scriber), which can also be said to be the relationship between the sender of the event and the receiver of the event. For example, there are a lot of magazines and magazines on the market. And I'm only interested in some of them, so I can offer a subscription to a magazine publisher. Then, whenever the magazine was released, I received the magazine I subscribed to in the magazine publisher. In this relationship, the magazine publisher is equivalent to the event issuer, and I am the event subscriber. Whenever a magazine is released, a release event is triggered.

In object-oriented language interpretation, the meanings of both are as follows:

Event Publisher (publisher)

It is an object and maintains its own state information. Whenever the state information changes, an event is triggered and all event subscribers are notified. For magazine publishers, every magazine has its own information in it, and when the magazine is published, I want to notify the person who subscribed to the magazine: The magazine has been issued, please pay attention to check!

Event Receiver (receiver)

This object wants to register its object of interest, which is to subscribe to its favorite magazine. In addition, this object typically provides an event-handling method that is automatically executed after the event Publisher triggers an event. For the example above, which is what I'm going to do when I get a magazine, for example, you can shout all over the place: I got the magazine! You can also collect the magazine slowly appreciate, how to achieve exactly depends on your own preferences.

Here is the. NET event-handling mechanism model:

Description: Multiple event handlers can be registered

C # event mechanism

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.