C # events and Delegation

Source: Internet
Author: User

In fact, C # events and delegation can solve problems without being used in the daily development process, but they are not used differently. They can better reflect the high cohesion and low coupling of objects. To ensure interoperability between the two objects, provides external interfaces, or even allows another object to process the operations of this object when a specified event occurs. For example, I think of myself as an object, the hotel owner is a different object. These two objects are completely independent. I don't know which restaurant I want to eat, and the hotel owner doesn't know who will eat; if no incident exists, I went to a hotel and told my boss that I was about to eat. The boss didn't talk back. I said that I was about to eat cabbage ..... and so on. I am operating on my own. This is too rigid. I don't know if this hotel has any food I ordered and no feedback is provided. Normally, I should have arrived at the hotel, the boss triggered an event on his own. The incident was when he saw a customer come to dinner and asked me what to eat, instead of coming to a "vacuum" Hotel; events are a process of transmitting information. C # itself is an object-oriented language. We need to think about programming with the laws of nature; if you don't need the event, you can also implement the above-mentioned meal problem. This is tedious and troublesome, and does not conform to the object-oriented idea. You come to the hotel and then call the boss's method:

 

Public bool (food name, quantity); // method provided by the boss

If (boss. Dinner (beef, 10) {this House has beef;} // I call the boss's meal method to determine if I want the food;

 

In this way, no matter who is going to eat, you must provide a method for external calls. Everyone has a habit of eating, and the ordering habits of different numbers cannot be fully understood, in this way, a problem arises, with poor interaction, no communication, and no response. If there is an event, it will obviously become more humane, and there is no need to consider too much outside, the characteristics of this store are defined as process events. The special steps must be like this. The characteristics of this hotel are defined as process events, and customers come to follow these event procedures, it is very accurate to know what customers want to eat, and the incident is a good channel for mutual communication. People First ask what tastes to eat. This is an event if the customer says that I don't want to eat it, the boss's event parameters have not received any input from the customer. I will explain the object event in the following example;

 

Public class owner

{

Public Delegate void saw a customer coming (string Hello); event Delegate, which will be discussed later;

Public event saw a customer come to dinner; // This is an event;

Public void on (string info); // Method for triggering an event;

}

Public Class Customer

{

Boss laoban = new boss ();

Laoban. Customers come to dinner + = new customers (Handling boss events );

Public void handles boss events (string Hello)

{

MessageBox. Show ("Hello, do you have any beef? ");
}

Laoban. on customers come to dinner ("what do you want to eat?"); // This method may be called after an event is triggered, generally not triggered in the subscription object;

}

 

I simply subscribe to the boss's events, and then the boss asks me what to eat. Do I give back? This is just a metaphor and may be a bit inappropriate. The following describes the relationship between delegation and events;

The delegate literal understanding is to help with the work. The intermediary means that the delegate is used to associate methods and events in the Code. When an event is triggered, you can call all entity Methods bound to the delegate chain one by one through the delegate chain. The delegate chain:

Comrades who have read the data structure know the structure of the linked list. Every data field is stored and called as follows:

Laoban. The customer came to dinner + = new saw a customer (Handling boss Event 1); // This is to bind a method to a data domain of the delegate chain;

Laoban. The customer came to dinner + = new saw a customer coming (handling the boss incident 2); // You can bind multiple methods to one delegate chain consecutively to enable multicast delegation;

In the preceding figure, four methods are bound to the delegate chain. When the delegate chain method is called, the delegate chain will call the method on the delegate chain in the order it was originally bound.

"Handle boss Event 1 method", "then call handle boss event 2"; the call parameters of the delegate chain are passed down, that is, the results obtained by calling the first method will continue to be passed down to the second method. If the input and output parameters are not modified during method execution, the parameters will not be affected, this is for the reference type;

An event is a process of triggered action. For example, if you say a word to me, my ears will receive it, and then I will handle it internally to determine what event to trigger; delegated events are events, which have no relationship with each other, except that events use the characteristics of delegated events to help handle events; when using the delegate, we must first determine the delegate type. What is the type is to determine the physical structure of the code and define the delegate:

Delegate void delegatename (); // delegate is the keyword of delegation. delegatename is the name of the Delegate. Defining the delegate type is to determine the data structure type stored in the data domain in the delegate chain, when learning the data structure, we need to first define the data types of no node. The principle is similar. After determining the type, it is a specific instance:

Event delegatename eventname; event instances are defined using event keywords and delegate types. In fact, event instances are delegated instances. Each object has a meaningful meaning. It seems that continuous calling of methods can be achieved through delegation, why do we need events? Some things do not seem as simple as they are on the surface. The existence of events is related to the physical structure. When the CLR executes the delegate and the event processing process is different, the event is the event;

 

Here is the event and Commission. I hope it can help you further understand it. The above is my own understanding. If there are any mistakes, please kindly advise. Thank you;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.