Not long after I started learning. net, I began to focus on custom events, delegation, and other knowledge points.
Since the project has been relatively small, these knowledge points have never been used, so it has always been useless.
Previously, the information found on the Internet was too theoretical. For example, an event can pass a method as a parameter, and the event only declares the method to be called (the specific call is not certain ).
Bytes --------------------------------------------------------------------------------------------------------------------------
Today, I am going to look for information on the Internet to review the inheritance of classes. In the examples I have used custom events, and they are very simple custom events.
SortedCodeAs follows:
Protected Void Page_load ( Object Sender, eventargs E)
{
Book B = New Book ();
B. renchenged + = New Book. renchenghender (B _renchenged );
Response. Write (B. Price. tostring ());
}
Private Class Book
{
Public Delegate Void Renchenghender (); // Define a delegate
Public Event Renchenghender renchenged; // Define an event
Private Float Price = 1 ;
Public Float Price
{
Get
{
Renchenged (); // If this value is read, the event will be triggered.
Return Price;
}
}
}
Private VoidB _renchenged ()
{
Response. Write ("Read price <br/>");
}
Now the problem occurs. In the above example, I can call the B _renchenged () method directly when getting the price attribute instead of using custom events in this way, isn't that easier?
This is a problem that has plagued me for a long time.
Suddenly, today I suddenly want to understand that, in terms of the implementation of the above functions, the results of the two methods are indeed the same, it seems that the direct call of B _renchenged () the method is simpler than using custom events.
However, I think the difference is: who specifies the B _renchenged () method when the price attribute get is used?
If the entire code is written by only one person without considering other factors, you can directly call the B _renchenged () method.
However, if the person who writes the book class is mr. A, and the person who writes the page_load code is mr. B. Mr. A only designs that an event can be triggered when the price attribute is get, but does not matter what the event is. Mr. B only needs to know what method to execute when get price is not concerned with its internal implementation (for example, the commonly used protected void button#click (Object sender, eventargs E) just use it. You don't need to know how it works)
In this way, everything seems to be clear. The main difference between them isArticleIn addition to those mentioned at the beginning, there is a division of labor between the trigger events. I hope I can give some help to people like me who cannot tell the relationship between the two.