Introduction
Articles about events and delegates have seen a lot, but they are always incomplete. Especially when explaining to others is always very fragmented, so in this sort of, but also for everyone's reference, very please enlighten us.
This article from how a subclass triggers the base class of events (event), leads to the commonality and distinction between events and delegates. After a brief analysis of the reasons behind the underlying reasons, we also provide some reference materials. Welcome to leave a message to discuss.
Problem
"What if I want to trigger the event of the parent class in the subclass?" "(You can do it yourself or think about it, and then continue to see if you've ever had a technical misunderstanding)
The problem is broken down into two steps:
- The parent class defines an event
- The subclass triggers the event that is defined in the parent class
The original idea was:
classBaseClass { Public Delegate voidCompletedeventhandler (); Public EventCompletedeventhandler workcompleted; Private voiddosomethinginbase () {//Do something and notify other class if(workcompleted! =NULL) {workcompleted (); } } } classSubclass:baseclass {Private voiddosomethinginsub () {//Do something and notify other class if(workcompleted! =NULL) {workcompleted (); } } }
Do you think the above code will be fulfilled?
When I ask, we should start to wonder. Yes, this implementation stops in the idea of actually not working, the compiler will give the following error prompt:
The event ' baseclass.workcompleted ' can only be appear on the left hand side of + = or-= (except when used from within the Ty PE ' baseclass ')
Solution to the problem
I give my answer (hopefully we can also give a better version):
classBaseClass { Public Delegate voidCompletedeventhandler (); Public EventCompletedeventhandler workcompleted; Private voiddosomethinginbase () {//Do something and notify other classraisecompletedevent (); } protected voidraisecompletedevent () {if(workcompleted! =NULL) {workcompleted (); } } } classSubclass:baseclass {Private voiddosomethinginsub () {//Do something and notify other classraisecompletedevent (); } }
Explanation of the problem
Why did the problem arise?
In fact, the question of a change in the law, you may directly answer it? That is, "what are the differences and connections between events and delegates?" ”
I also plan to explain the problem from this perspective:
Explain the reason for the compiler error in a word first
An event is a special delegate, or a restricted delegate, that is a special application that only imposes +=,-= operators. They are essentially a thing.
Problem mining starts from the Commission: How to define a delegate?
Delegate // delegate is equivalent to the class keyword, and function name corresponds to the defined delegate type. Delegate void Action ();
How do I create a delegate instance?
Action Action;
How are events defined and instantiated?
Delegate void Actionhandler (object sender, EventArgs args); // define the delegate type first Event Actionhandler ActionEvent; // Defining Event Instances
To the head, the definition of the event and the definition of the delegate are the same, which again confirms the consistency of the "event and delegate" form.
However, there is an important difference between an event and a delegate.
Event vs. Commissioned
Compile to create a private delegate example, and apply the Add, remove method on it.
To illustrate the difference, a delegate is added to the sample code, with the following code:
classBaseClass { Public Delegate voidCompletedeventhandler (); Public EventCompletedeventhandler workcompleted; PublicCompletedeventhandler workcompleteddelegate; Private voiddosomethinginbase () {//Do something and notify other classraisecompletedevent (); if(Workcompleteddelegate! =NULL) {workcompleteddelegate (); } } protected voidraisecompletedevent () {if(workcompleted! =NULL) {workcompleted (); } } } classSubclass:baseclass {Private voiddosomethinginsub () {//Do something and notify other classraisecompletedevent (); if(Workcompleteddelegate! =NULL// will not be the same as the event in compile times error {workcompleteddelegate (); } } }
ILDASM View the compiled structure, you can see that the event has been assigned to two methods (in fact, because the compiler has been castrated, do not let other classes directly invoke the event, but delegate is sound)
The Stone of his mountain
Delegates and events support static methods and member methods, delegate (void * pthis, f_ptr), when a static rebate method is supported, pthis NULL. When the member method is supported, Pthis passes the notified object.
The three important fields in the delegate object are pthis, F_ptr, Pnext, which is the notification object reference, the function pointer/address, and the next delegate node of the list of delegates.
SOURCE download
Download
Summarize
The delegate keyword is analogous to the Class keyword, which defines a delegate type and then creates a delegate instance.
When you create a delegate instance, the event keyword is used to decorate it to create an incident. That is, an event is a special delegate.
Delegate commonly used to express callbacks, events to express outgoing interfaces
(The event is a castrated version of the delegate, only to the operation of +=,-=, can not go out to cheat (by others invoke him/her), do not know my understanding is accurate, but also please you pass the Warrior enlighten)
Reference
The relationship and difference between events and delegates (the article is short but the content is real)
C #: Delegates and custom events (underlying content)
C # Comprehensive disclosure--in-depth analysis of delegates and events (a lot of content, involving a wide range)
A practical tool for initial knowledge of Ildasm.exe--il anti-compilation
Events and Delegates (event vs. Delegate)-Delegate of the event castrated edition