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

Source: Internet
Author: User

In the preface, you should have a little understanding of the importance of delegation and events. Although we can still be vague, from my vernacular series, I will make these concepts easy to understand. First, let's talk about delegation first. Literally, as long as the Chinese people know this meaning, unless the two Chinese characters do not know it, for example, james commissioned John to buy a ticket.
 
But is it that simple in our program world? Of course, what is OO? Oo is the abstraction of the real world. Everything in the real world can be well interpreted in OO. Sorry, it's a long journey ......

Let's just go straight into it. If you are not bothered, you can take a look at the greetingpeople example in Chinese and English on the Internet.

 

C # syntax

Public Delegate void bugticketeventhandler ();

 

Delegate is the keyword. [Note: eventhandler is a naming standard for Microsoft C # commissioned by the declaration. My habit is that the standard should be mentioned in the first time, and this naming habit should be developed in the first time]

This is the Commission. It's so simple. What is the difference between it and our general method?

 

Public void buyticket ()
{
Method body
}

 

Let's see the difference. In real life, delegation is just a command to do things for others. In the program world, delegation only stores the addresses of various methods, he does nothing himself.

Then, let's look at the real-life scenario that James commissioned Mr. Zhang to buy a ticket. How can this be reflected in the program world?

 

 

Code

// Small Zhang class
Public class mrzhang
{
// In fact, the person who is sad about buying a ticket is John
Public static void buyticket ()
{
Console. writeline ("nnd, let me buy a ticket every time, chicken! ");
}
}

// James class
Class mrming
{
// Declare a delegate, which is actually a "command"
Public Delegate void bugticketeventhandler ();

Public static void main (string [] ARGs)
{
// Here is what this command is for. In this example, mrzhang. buyticket "buy a ticket for John"
Bugticketeventhandler mydelegate = new bugticketeventhandler (mrzhang. buyticket );

// At this time, the delegate is attached with a specific method
Mydelegate ();
Console. readkey ();
}
}

 

 

Bugticketeventhandler mydelegate = new bugticketeventhandler (mrzhang. buyticket );

This is the delegate declaration method. bugticketeventhandler (delegate method); the delegate method must be added because the Delegate constructor is not empty.

Note: The delegate parameters and return types must be consistent with the delegate method. For example:

Public Delegate void bugticketeventhandler ();

Public static void buyticket ()
{
Console. writeline ("nnd, let me buy a ticket every time, chicken! ");
}

Students, here we can digest and take a rest. Let's briefly talk about the concept of the delegated chain:

In fact, the delegated chain is quite simple. In real life, James asked Mr. Zhang to buy a ticket and may ask him to bring a movie ticket,

In our program world, the statement is:

 

// Small Zhang class
Public class mrzhang
{
// In fact, the person who is sad about buying a ticket is John
Public static void buyticket ()
{
Console. writeline ("nnd, let me buy a ticket every time, chicken! ");
}

Public static void buymovieticket ()
{
Console. writeline ("I'll go, pick up a girl myself, and let me bring movie tickets! ");
}
}

// James class
Class mrming
{
// Declare a delegate, which is actually a "command"
Public Delegate void bugticketeventhandler ();

Public static void main (string [] ARGs)
{
// Here is what this command is for. In this example, mrzhang. buyticket "buy a ticket for John"
Bugticketeventhandler mydelegate = new bugticketeventhandler (mrzhang. buyticket );

Mydelegate + = mrzhang. buymovieticket;
// At this time, the delegate is attached with a specific method
Mydelegate ();
Console. readkey ();
}
}

 

In fact, we just added mydelegate + = mrzhang. buymovieticket in the program;

At this time, this Commission is equivalent to doing two things: first buying a ticket, then buying a movie ticket!


Now, the delegation of the vernacular is introduced here. Isn't it as terrible as everyone thinks? In the next session, Let's explain the event of the vernacular.

We recommend two articles about life.

Why can't we do it]

Why can't we do it [employee]

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.