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

Dear Experts,

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

  http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21840188.html

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"?  

Thanks!
--BrianMc1958
BrianMc1958Asked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
AlexFMCommented:
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.
0
 
BrianMc1958Author Commented:
OK.  This is fascinating.  I'm very surprised that there is no direct callback.  I'll post yet another follow up...

Thanks!
--BrianMc1958
0
 
AlexFMCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.