. NET 0 Basic Primer 05: Delegates and Events

Source: Internet
Author: User

A: Preface

This section, we need to stop our small game development, although it is not perfect now, is very simple, even there are bugs. But to get a better understanding of C #, it's time to delve into the basics.

Of course, in fact, this section is still a bit difficult for beginners with 0 basic primer. Delegate and event, if only a general say, may be one or two words. However, our curriculum follows a principle: know it and know why. So, this section is actually a bit more, but I want you to savor it and try to digest the two concepts of entrustment and events.

At the same time, the course also teaches you to use a tool to analyze the code.

Second: Commissioned by the reality of the scene: the seller entrusted to express delivery?

Anything, first of all, ask a why. The question here is: Why do sellers have to entrust express delivery?

Because he can't do it. He sold a pot in Beijing to Shanghai, he should not, and could not be able to run to Shanghai to give others. So he wants to "entrust" to others to do this thing.

In the world of code, too, our game of mouse, now all the logic code in the background of the form, as we learn object-oriented, the code is refactored, the business logic code will be placed in other classes, this "other class" is responsible for "game start" logic, where "game start" There is a link in the logic to show "game start" on the interface. Because the business class does not own the form, it does not show "game start" and this time the "delegate" comes in handy. That is, the business class should not and will not be able to display the "game start" directly to the form, so it "delegates" the form to show "game start".

Three: What is a delegate?

Let's go back to the seller's example of selling a pot to describe: "What is a delegate". In fact, the seller commissioned courier delivery, the following code is required:

The question of "What is a delegate" is a representation, in fact it hides 5 small problems:

1: What is a delegate statement?

2: What is a delegate variable?

3: What is a delegate variable assignment?

4: What is a delegate processor?

5: How do I invoke a delegate?

In the code, the sequence number of these 5 minor questions corresponds to the number part of the figure,

First, the procedure has to have a place to declare the Commission;

Second, the seller himself has to define a delegate variable;

Again, by some form to the seller's delegate variable assignment, what is this value, is a delegate processor "reference." The delegate processor is a method, so the process of assigning a value is to assign a reference to a method to a delegate variable.

Finally, invoking the delegate, in effect, is using a delegate variable.

Note that the return values and parameters of the delegate declaration and the delegate processor must be consistent.

Four: What is the nature of the mandate?

What we have described above is what the Commission is, and what we are actually merely elaborating is what is the representation of the delegate, so what is the nature of the delegate?

So far, we've learned two types of concepts: classes and enumerations. What we want to emphasize here is that the "Sendproducthandler" in the delegate declaration of the above code is actually a class, and we should look at it as:

Internal class SendProductHandler:System.MulticastDelegate
{
Public Sendproducthandler (Object @object, IntPtr method)
{
}

Public virtual IAsyncResult BeginInvoke (string product, AsyncCallback callback, Object @object)
{
}

public virtual void EndInvoke (IAsyncResult result)
{
}

public virtual void Invoke (string product)
{
}
}

Why is this, I believe you see the following video will understand.

V: Video

For non-public sections, please contact the most popular course (www.zuikc.com) to watch.

VI: What is the incident?

Knowing the delegate, let's take a look at the event, the section "Two: What the delegate is" in code 2 and 3, modified to:

The "delegate" becomes an "event".

After using the event, you can add multiple methods to your delegate variable _sendproduct, you see, in the code position 3, no longer a "=" number, but a "+ =" number.

. NET 0 Basic Primer 05: Delegates 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.