. Net difficulty 1: Events and delegation (witty Version)

Source: Internet
Author: User

Many of my friends have a headache in learning C #, namely, events and delegation. flexible application of events and delegation is a key to improving the. NET level,
How to correctly understand events and delegation is fundamental. In this article, I try to explain the event and Commission to you in a humorous and plain way. I have a deep understanding and a deep understanding of it.
What are events and delegation.

Story 1: in modern society, the pace of life is too fast. Many people are busy with their work and have no time to worry about their feelings. Therefore, especially in the IT industry, there are many diamond old five. Haha,
So now we have a training course on how to talk to a strange girl. Now simulate the entire training process [this story comes from SohuArticle].

Phase 1: teachers should teach everything.
Student A: How can I talk to a lively beauty? INSTRUCTOR: conversation method (lively beauty type)
Students: how to contact quiet-style girls? INSTRUCTOR: conversation method (quiet beauty type)
Student C: how to contact a professional white-collar beauty? INSTRUCTOR: conversation method (beauty type, professional white-collar)
........................
As a result, the students started to experiment.
Public void Experiment (beauty)
{
If (beauty A. type = lively type)
{
Conversation method (lively beauty type );
}
Else if (beauty A. type = quiet type)
{
Conversation method (quiet beauty type );
}
Else if (beauty A. type = Professional white-collar)
{
Conversation method (beauty type, professional white-collar );
}
.....................
}

Stage 2: I learned how to apply it comprehensively. I will use several methods taught by the teacher at the same time. Because of the diversity of people's personalities, there is only one way to the same beauty, so some people have learned to use multiple methods for a person.
If you follow the level of phase 1, the teacher has to provide the following methods.

Public void method 1 (wenjing and professional white-collar professionals of a certain type of beauty)
{
Conversation method (quiet beauty type );
Conversation method (beauty type, professional white-collar );
}

Public void method 2 (active and professional white-collar professionals of a certain type of beauty)
{
Conversation method (lively beauty type );
Conversation method (beauty type, professional white-collar );
}


The student started the experiment again.

// This time we met beauty B, which is a comprehensive combination of wenjing and professional white-collar workers.
Method 1 (beauty B );

// Beauty C is a lively and professional white-collar type. You have to write another method.
Method 2 (beauty C)

// If you encounter beauty D, it is... type, you have to write another method 3.
Method 3 (beauty D)

In the long run, can we write something like a class? (delegate is a special class and I will summarize it for you at last) to represent all the methods (note that this class is used to represent the method, the major is the reference of the method, or the pointer to the method ). Of course you can,ProgramThe highest mission of members is to achieve it, and there is nothing impossible. So we define a delegate (Special Class) to represent the method.

 Delegate of the Public Delegate void method (a beauty type); [Note: Why do you need to understand the delegate of the method literally, this is the method to delegate the actual execution.]

In this way, the teacher will only tell him the following method.

Public void: The Conversation method provided by the instructor (a method is provided for a beauty type or a method)
{

}

So how do students do it?

Public void Experiment (beauty type B beauty)
{
// The first is the quiet type
Delegate the B-type method of the conversation to delegate the method = the method of the conversation (the quiet type of the beauty );

// The second is for white-collar professionals
The method for entrusting the B-type beauty conversation to the Implementation + = the method for chatting (the beauty-type professional white-collar );

// After adding the method you want to delegate, start to bubble the mm
The method provided by the teacher (the method for entrusting B-type beauty to engage B-type beauty );
}

I saw it, but I met a pretty girl. If you want to use any method, you don't need to use if-else to judge it. That's good.

Stage 3: The Blue stage is better than the blue stage, and you will also create methods that the teacher does not have to soak in. This time, we fixed a reference to the delegate we just mentioned. (In the previous example, we wanted to declare a delegate for each mm, and then add a method to the delegate, I use this fixed delegate to add methods.

 Delegate session events of the public event conversation method [Note: it is actually a reference to a fixed delegate, which will be used later and cannot be defined by yourself];

In this case, you don't need a teacher. You will create it yourself and leave the mountains.

Public void Experiment (beauty Type Super beauty m)
{
Conversation event + = delegate the new conversation method (self-created conversation method 1 (Super beauty m ));
Conversation event + = delegate the new conversation method (self-created conversation method 2 (Super beauty m ));
Conversation event + = Delegate of new conversation method (self-created conversation method 3 (Super beauty m ));

// How many methods do you want to use?
.............................
}

Private void self-created conversation method 1 (beauty type M)
{
// Give full play to yourself
}

Method 2 created by private void (beauty type M)
{
// Give full play to yourself
}

Method 3 created by private void (beauty type M)
{
// Give full play to yourself
}

Summary:A delegate is an abstraction of a method, and an event is a fixed reference to a delegate. This delegate is often used as an example.Just like our button event, a button clicks to call a method, B button clicks to call a method, and then we abstract the button clicks to a fixed click event, then you can add any method you want to add. The Methods written in this article, specifically the client (call end) or the server end, may be confusing. The conversation methods are taught by teachers (teachers and servers ), except those created later. All the experiment methods are client (call end ).

added: I was also going to prepare a story for you. I also want to discuss the delegation and events in combination with the design model. I didn't expect the first story to occupy the layout. I think it's almost the same for everyone to understand this. Then, let's take a look at the articles about delegation and events in other blogs. I believe you will be able to break through this difficult point. Wish you success! thank you for your criticism and correction!

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.