NEWBIE: Do events and delegates inform, or do?

Posted on 2006-05-05
Last Modified: 2010-04-16
Dear Experts,

I'm trying to understand the basics of delegates and events in C#.

Assume I have two classes which interact with a delegate/event model.

ButtonClass:  Encapsulates a button, can be clicked.

CaresAboutClicksClass:  Does something when the button is clicked.

I had (vaguely) assumed that when ButtonClass registers a click, it somehow just broadcasts this information, and CaresAboutClicksClass somehow hears the broadcast--perhaps with an assist from the .NET runtime.

What I'm reading seems to say, though, that  CaresAboutClicksClass actually passes a METHOD--which is now a full-fledged object--to ButtonClass, initially.  Then, when ButtonClass registers a click, it doesn't somehow just TELL CaresAboutClicksClass that a click happened.  ButtonClass actually INVOKES the METHOD DIRECTLY.  CaresAboutClicksClass doesn't actually DO anything at that point.

Is that right?

If it is right, does it make any sense to ask "where is this method being called FROM?".

Question by:BrianMc1958

    Author Comment

    A shorter version of the same question:

    The subscriber isn't really saying, "When this happens, TELL me".  The subscriber is saying, "When this happens, DO this".

    Is that right?
    LVL 9

    Accepted Solution

    Multiple methods can be subscribed to an event. When the event happens, the CLR calls every method in undefined order.
    The important thing is that the event handling process is done by the intermediation of the CLR, not by direct callback.
    LVL 52

    Assisted Solution

    by:Carl Tawn
    Basically the ButtonClass would expose an event. CaresAboutClicksClass registers to be notified about the event by passing a method that matches the signature of the delegate associated with the event. When ButtonClass raises the event it calls the correct method of any object that has subscribed to the event.

    Assisted Solution

    That is right. ButtonClass has a "Click" event, which is a function pointer to keep things simple. So when the button is clicked, whatever function the "Click" function pointer is pointing to will get executed.

    Consider this pseudo-code:

    Button.Click = someOtherObject.TakeCareDaClicks();

    So when the button is clicked, someOtherObject.TakeCareDaClicks() is executed because the Click function pointer is pointing to it.
    LVL 7

    Assisted Solution

    A delegate is a typesafe method pointer.

    The event handling code in CLR will  call the methods, as indicated by delegates, when required.

    So neither of the classes originally mentioned above does the actual calling -- even the Click event of the button is handled in the same way (Delegates can be multicast and result in multiple method called upon invokation.)
    LVL 96

    Assisted Solution

    by:Bob Learned

    Author Comment

    Thank you, everybody.  That helps.  And I'll post a follow-up...


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now