C # delegation and event explanation in the vernacular series (2)

Source: Internet
Author: User

What is an event? Event? Click Event? Load events? A series of vague concepts impact our weak head

Let's take a look at the more orthodox ideas:

An event is a way to provide notifications when something is concerned with it.

Generally, events involve two roles.

Event Publisher ):The sender of an event, also known as the sender, is actually an object. This object maintains its own State information and triggers an event when its state information changes, and notify the event subscriber.

Subscriber ):The object that is interested in the event, also known as the handler, can register the event of interest. After the event publisher triggers an event, this code is automatically executed.

In order to better understand the above concepts, I will not talk about anything. Let's look at a simple code:

 

Code

// Publisher (publiser)
Public class publisher
{
// Declare a publication delegate
Public Delegate void publisheventhander ();
// Under the delegated mechanism, we establish a publishing event
Public event publisheventhander onpublish;
// The event must be triggered in the method. The publisher publishes a new book method.
Public void issue ()
{
// If someone registers this event, that is, this event is not empty
If (onpublish! = NULL)
{
Console. writeline ("the latest issue of Naruto is published today! ");
Onpublish ();
}
}
}

// Subscriber, rogue James
Public class mrming
{
// Events of interest, which refer to those of the publishing house
Public static void receive ()
{
Console. writeline ("Ga ga, I have received the latest version of" Naruto !! ");
}
}
// Subscriber, James
Public class mrzhang
{
// Events of interest
Public static void receive ()
{
Console. writeline ("Naive, so big, but also watch Naruto, Sb James! ");
}
}

Class story
{
Public static void main (string [] ARGs)
{
// Instantiate a publishing house
Publisher publisher = new publisher ();

// Register a subscriber of interest for this Naruto event. In this example, James
Publisher. onpublish + = new publisher. publisheventhander (mrming. Receive );
// Another event registration method
// Publisher. onpublish + = mrming. receive;

// The publisher triggers the publishing event of Naruto.
Publisher. Issue ();

Console. readkey ();
}
}

The compilation result is as follows:

 

 

If you look at the topic from top to bottom, I want to know what the publisher is and what the subscriber is. What about the event?

Let's first look at this sentence.

Publisher. onpublish + = new publisher. publisheventhander (mrming. Receive );

This is the Naruto that James subscribed to as he liked from the publishing house. Because John did not subscribe to it, he did not receive the book,

 

Let's take a closer look at this assignment statement. Have you ever been familiar with it? Yes. In the previous article, when talking about the delegate statement, it was just a token.

Delegate Value assignment:

Bugticketeventhandler mydelegate = new bugticketeventhandler (mrzhang. buyticket );

Therefore, do not be afraid of events. In fact, the essence of an event is a delegated chain,

Let's take a look at the event statement:

// Declare a publication delegate
Public Delegate void publisheventhander ();
// Under the delegated mechanism, we establish a publishing event
Public event publisheventhander onpublish;
When using an event, we must declare the corresponding delegate, and trigger the event is actually using the delegate chain.

Now, let's digest the content. In the next lecture, we will talk about the long-awaited sender and E mysterious parameters.

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.