. NET zero-basic entry 05: Delegation and events,. net entry 05 Delegation

Source: Internet
Author: User

. NET zero-basic entry 05: Delegation and events,. net entry 05 Delegation

I. Preface

In this section, we need to stop our game development. Although it is not perfect yet, it is very simple and even contains bugs. However, to better understand C #, it is time to gain an in-depth understanding of some basic knowledge.

Of course, in fact, the content of this section is still a bit difficult for beginners who have no basic knowledge. Delegation and event. If you just talk about it in a general sense, it may be a sentence or two. However, our courses should follow the principle of knowing, knowing, and so on. Therefore, the content of this section is actually a bit more, but I hope you can savor it and try to thoroughly digest the concepts of delegation and events.

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

 

Ii. Actual scenario of delegation: What is the actual scenario for the seller to deliver the goods via express delivery?

First of all, you must ask why. The question here is: Why do Sellers need to entrust express delivery?

Because he cannot do it. He sold a pot in Beijing to Shanghai. He should not and cannot deliver it to others. Therefore, he wants to "delegate" to others to do this.

In the world of code, this is also the case. In our mouse-hitting game, all the logic code is now on the background of the form. As we learn object-oriented code and refactor the code, the business logic code is put into another class. This "other class" is responsible for the logic of "Game start, one step in the logic of "Game start" is to display "Game start" on the interface. Because the business class does not have a form, it cannot display "Game start", and "delegate" will be used in this case. That is, the business class should not and cannot directly display "Game start" on the form. Therefore, it "delegates" the form to display "Game start ".

 

Iii. What is delegation?

Let's go back to the seller's example to describe "what is the delegate ". In fact, the seller needs the following code to entrust express delivery:

The question of "What is delegation" is a representation. In fact, it also hides five minor issues:

1: What is the delegate statement?

2: What is the delegate variable?

3: What is a delegate variable assignment?

4: What is a delegated processor?

5: How do I call a delegate?

In the code, the serial numbers of the five minor problems exactly correspond to the numbers in the figure,

First, the program must have a place to declare delegation;

Second, the seller has to define a delegate variable;

Again, the seller's delegate variable is assigned a value in some form. What is this value? It is a "reference" of a delegate processor ". The delegate processor is a method. Therefore, the process of assigning a value is to assign a reference to the delegate variable.

Finally, the delegate variable is actually used to call the delegate variable.

Note: The delegate Declaration must be consistent with the return value and parameter of the delegate processor.

 

4. What is the nature of delegation?

In the above section, we have explained what delegation is, and what is actually the representation of delegation, so what is the nature of delegation?

So far, we have learned two types of concepts: class and enumeration. Here we should emphasize that the "SendProductHandler" in the delegate Declaration of the above Code is actually a class, which should be viewed:

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? I believe you will understand the video below.

 

V. Video

For the non-public part, contact www.zuikc.com.

 

6. What is an event?

Now that we know the delegate, let's look at the event and modify code 2 and 3 in section 2: What is the delegate:

"Delegate" becomes "Event.

After using the event, you can add multiple methods for your delegate Variable _ sendProduct. As you can see, the position 3 in the code is no longer a "=, it is a "+ =.

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.