Commissioned:
using a delegate we can pass the method as a parameter, and the delegate can be understood as a pointer to the method.
Event:An event can only be called inside the type that declares it, externally, only events can be registered and unregistered, i.e. only + = and-= Operations can be performed
the difference between a delegate and an event:A delegate is a type that can be declared at the class level (that is, it can be declared directly in a namespace), whereas an event is an object that can only be declared internally by a type, in fact the event is an instance of a delegate and is a special delegate that has a functional limit.
the composition of the event:The interior of the event is actually made up of a private delegate and a add_xxx and remove_xxx method,As shown below, after a delegate is defined and the corresponding event is deserialized, you can see that its internal composition is a private mydelete type of delegate myevent and add_myevent with remove_myevent two parties Method
the + = and-= Operations of the event:the + = and-= operations of the event actually invoke the Add_xxx method in the event and the Remove_xxx method, merging and removing the delegate in time, such as invoking the + = operation in the MyEvent event, actually calling the Add_myevent method, In this method, the delegate in the event is merged (call the Delegate.combine method)
Declaration and use of a delegate:
the inheritance structure and composition of a delegate:
= = Comparison of Delegates:two delegates of the same type are compared with the argument list in the delegate, only two delegates have the same type as the method list, and two delegates do the = = operation to return the true,== comparison, or the Equals method is called:
is the delegate a reference type? from the delegate inheritance hierarchy, you can see that the delegate is a reference type, but the instance of the delegate with the + = and-= operation does not affect another delegate instance that points to it, as shown in the following code. 1, from the memory point of view, in the initial case myDelegate1 and myDelegate2 in the stack address and the address of the heap is the same, figure: when the + = operation is performedon the myDelegate1, the address of themyDelegate1 in the heap changes, andtheaddress of the MyDelegate2 object is unchanged. So when you call MyDelegate2 again, it's still calling the method in the original method list, and it's not affected by myDelegate1. 2, from the source code point of view, the above source code compiled after the actual generated code as follows: the + = operation of the delegate finally calls the Combine method of the delegate, and the Combineimpl method is called in the Combine method, While the Combineimpl method is a virtual method, its implementation is overridden by the delegate subclass Multicastdeleg?ate, in the Combineimpl method Newmulticastdelegate This method returns the merged delegate object, Combineimpl Method FragmentOverloaded Methods for Newmulticastdelegate: You can see in this method that the last delegate object returned is no longer pointing to the original delegate, it is already a completely new delegate object , so it does not change the object in the actual heap myDelegate1 this delegate variable, but instead points to another delegate instance object , the + = and-= operations do not affect multiple delegate variables that point to the same delegate object.
Delegates and events