What keeps track of delegates?

J

jm

If I have multiple subscribers to a delegate, what keeps track of them?
Are they out there on the heap?

When I declare a delegate:

public delegate void MyDelegate (object myObject, MyObjectEventArgs
myEvent);
public MyDelegate SomethingHappened;

and later I have two subscribers, what's going on in the background.
My Delegate "variable," if that is what it's called just looks like a
variable, but behind the scenes it can hold multiple instances of
subscribers, correct, like an array? Just wondered what was going on.
I'm "protected" from it by the .Net framework.

Thanks.
 
T

Thomas T. Veldhouse

jm said:
If I have multiple subscribers to a delegate, what keeps track of them?
Are they out there on the heap?

When I declare a delegate:

public delegate void MyDelegate (object myObject, MyObjectEventArgs
myEvent);
public MyDelegate SomethingHappened;

and later I have two subscribers, what's going on in the background.
My Delegate "variable," if that is what it's called just looks like a
variable, but behind the scenes it can hold multiple instances of
subscribers, correct, like an array? Just wondered what was going on.
I'm "protected" from it by the .Net framework.

I don't know the answer difinitively, but since it appears to be a classic
observer pattern, it is like a collection of delegate (class) instances on the
object being observed. Thus, if you listen to the events on a form, the
delegate instances are part of the form instance. THat is why it is good
practice to unregister your events when you are no longer interested in
listening for them.

Perhaps somebody else will answer more definitively.
 
G

Guest

The System.Delegate class from which your delegate inherits maintains an
invocation list for a delegate -- a list of methods that will be called when
the delegate is invoked.

When you invoke the delegate, the base class implementation loops through
the list and calls each method.

Peter
 
D

Dave Sexton

Hi Peter,

More accurately, it's the MulticastDelegate class, which derives from
Delegate, that provides the invocation list.
 
W

William Stacey [C# MVP]

And by default, they are executed syncronous and in list order. You can
invoke them yourself on a seperate threads if that is something you need.

--
William Stacey [C# MVP]

| Hi Peter,
|
| More accurately, it's the MulticastDelegate class, which derives from
| Delegate, that provides the invocation list.
|
| --
| Dave Sexton
|
| | > The System.Delegate class from which your delegate inherits maintains an
| > invocation list for a delegate -- a list of methods that will be called
when
| > the delegate is invoked.
| >
| > When you invoke the delegate, the base class implementation loops
through
| > the list and calls each method.
| >
| > Peter
| >
| > --
| > Co-founder, Eggheadcafe.com developer portal:
| > http://www.eggheadcafe.com
| > UnBlog:
| > http://petesbloggerama.blogspot.com
| >
| >
| >
| >
| > "jm" wrote:
| >
| >> If I have multiple subscribers to a delegate, what keeps track of them?
| >> Are they out there on the heap?
| >>
| >> When I declare a delegate:
| >>
| >> public delegate void MyDelegate (object myObject, MyObjectEventArgs
| >> myEvent);
| >> public MyDelegate SomethingHappened;
| >>
| >> and later I have two subscribers, what's going on in the background.
| >> My Delegate "variable," if that is what it's called just looks like a
| >> variable, but behind the scenes it can hold multiple instances of
| >> subscribers, correct, like an array? Just wondered what was going on.
| >> I'm "protected" from it by the .Net framework.
| >>
| >> Thanks.
| >>
| >>
|
|
 
G

Guest

In the delegates samples section of the .Net 1.1 framework SDK there's a chat
sample that manually calls methods from the invocation list. It's a great
tutorial on this topic.

On my PC, the sample is at C:\Program Files\Microsoft Visual Studio .NET
2003\SDK\v1.1\Samples\Technologies\DelegatesAndEvents\cs

I point that out because there are other chat samples in the SDK that do not
use the same technology.

Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top