NEWBIE: Are running delegates entirely independent of originating subscriber class?

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

I'm trying to understand delegates.  This is a followup from:

In my original question, I was told it was actually the CLR that calls the delegate when it's event happens.  My (crude) understanding of a delegate is that it's like a mini-class spawned from a subscriber class at run-time.  It's an independent, instantiated object.  (It doesn't seem to me that the original subscribing class even knows about a particular event.)

If that is so, when the delegate is called, does it know anything about the state of the original subscibing class?  Can it get any information from the original class at that moment?  Or is it "fire-and-forget"?  

Question by:BrianMc1958
    LVL 52

    Accepted Solution

    Its a fire-and-forget. The object that raises the event can pass information in the events arguments, but the object itself knows nothing about the object subscribing to the event.
    LVL 48

    Assisted Solution

    The only thing that event source knows about subscriver(s), is whether there is at least one subscriber:

    if ( MyEvent != null )
        MyEvent( <parameters> );

    Without this test, MyEvent call gives exception, if there is no subscribers.
    As mentioned by carl_tawn, event source doesn't know anything about subscribers. This is good thing, which allows to write classes without knowledge about their clients. Changing in client code doesn't affect event source class.

    Author Comment

    OK.  This is fascinating.  I'm very surprised that there is no direct callback.  I'll post yet another follow up...

    LVL 48

    Expert Comment

    Why there is no callback? You can get it using parameters. For example, when .NET raises FormClosing event, client can cancel closing by setting the Cancel property of the FormClosingEventArgs.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    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…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now