By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

What keeps track of delegates?

P: n/a
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.

Nov 17 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
jm <ne***********@gmail.comwrote:
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.

--
Thomas T. Veldhouse
Key Fingerprint: D281 77A5 63EE 82C5 5E68 00E4 7868 0ADC 4EFB 39F0
Nov 17 '06 #2

P: n/a
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.

Nov 17 '06 #3

P: n/a
Hi Peter,

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

--
Dave Sexton

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:6F**********************************@microsof t.com...
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.


Nov 17 '06 #4

P: n/a
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]

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:em**************@TK2MSFTNGP03.phx.gbl...
| Hi Peter,
|
| More accurately, it's the MulticastDelegate class, which derives from
| Delegate, that provides the invocation list.
|
| --
| Dave Sexton
|
| "Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
| news:6F**********************************@microsof t.com...
| 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.
| >>
| >>
|
|
Nov 18 '06 #5

P: n/a
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\DelegatesAndEve nts\cs

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

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA
"William Stacey [C# MVP]" wrote:
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]

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:em**************@TK2MSFTNGP03.phx.gbl...
| Hi Peter,
|
| More accurately, it's the MulticastDelegate class, which derives from
| Delegate, that provides the invocation list.
|
| --
| Dave Sexton
|
| "Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
| news:6F**********************************@microsof t.com...
| 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.
| >>
| >>
|
|
Nov 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.