Talk about. NET Events and delegates

Source: Internet
Author: User
Tags class definition object object

When it comes to. NET events, you may want to say that the tutorials are flying, what else can be said about a rotten thing? Yes, there are a lot of good articles profiling events like Mr. Zhang Ziyang's delegates and events in C #

Relive the Observer mode--water heater Change these two articles let me understand the delegate, the basics of event and observer patterns, additional in-depth event articles, and a blog Hall breaking trilogy, (btw these are all I have seen, if you have seen better articles please post for more people to learn, thank you.) :))

Now, the place where I feel I need to pay attention to the stuff that's been said to be rotten.

1 Unicast and multicast events

An explicit definition of the 2 event (which then explains the difference between the delegate and the event)

3. NET event model

For unicast and multicast event concepts. The information is defined in this way: A unicast event is an event notification issued by an object (class) that can only be handled by an external event handler, and not by multiple event handlers, which are event notifications from objects (classes) that can be handled by different event handlers at the same time.

To say it is so simple, to understand clearly is not very simple. Have you ever wondered how this was actually done? Here I have read the. NET Framework Program design 17.5 The history of commissioned: System.Delegate and System.MulticastDelegate, if interested can look, system.multicastdelegate defined in FCL inherited from System.Delegate, here Multicastdel Egate is actually multicast commissioned, then multicast events are implemented through this, needless to say delegate everyone can guess is unicast commissioned, then we usually define a delegate public delegate void back (Object value, Int32 Item, Int32 NumItems)

When the compiler encounters this delegate definition, it produces a complete class definition:

public class Back : System.MulticastDelegate
{
public Back(Object target, Int32 methodPtr);
public void virtual Invoke(Object value, Int32 item, Int32 numItems);
public virtual IAsyncResult BeginInvoke(Object vlaue ,Int32. numItems, AsyncCallback callback,Object object);
public virtual void EndInvoke(IAsyncResult result);
}

The internal method of this class does not understand that it does not matter, first look at this class of the back is inherited from System.MulticastDelegate, that is, we usually define the delegate is almost inherited from multicast commissioned, then why there is unicast commissioned, the ". NET Framework Program Design" Said to be a flaw in the Microsoft. NET Framework design. So here we remember that the general definition of the delegate is basically multicast, that is, can be used + + operation of the delegation into the chain, here I cannot but the treasure of multicast and unicast understanding is wrong.

The explicit definition of the event, perhaps you do not know what the explicit definition is, I believe many friends usually define their own events also did not pay attention to this problem.

How do we define events in our daily memory? is not the following look:

classMailManager
{
//定义一个委托类
publicdelegatevoidMailMesgEventHandler(Objectsender,EventArgse);

//定义对应委托的事件
publiceventMailMesgEventHandlerMailMsg;
}

We need to first define a delegate class for the event (here EventArgs i omit not defining a particular subclass of myself), and then use this delegate type to define the event.

Look very simple, yes, here is the implicit definition of the event, why is it called implicit, my own name haha, compile this event to define the code to produce more code, as follows these simplified pseudo code:

private MailMesgEventHandler MailMesg = null;
public void add_MailMesg(MailMesgEventHandler handler)
{
MailMesg = (MailMesgEventHandler);
Delegate.Combine(MailMsg, handler);
}
public void remove_MailMesg(MailMesgEventHandler handler)
{
MailMesg = (MailMesgEventHandler);
Delegate.Remove(MailMsg, handler);
}
privateMailMesgEventHandlerMailMesg=null;
publicvoidadd_MailMesg(MailMesgEventHandlerhandler)
{
MailMesg=(MailMesgEventHandler);
Delegate.Combine(MailMsg,handler);
}
publicvoidremove_MailMesg(MailMesgEventHandlerhandler)
{
MailMesg=(MailMesgEventHandler);
Delegate.Remove(MailMsg,handler);
}

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.