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.
Thomas T. Veldhouse - 17 Nov 2006 21:53 GMT
> If I have multiple subscribers to a delegate, what keeps track of them?
> Are they out there on the heap?
[quoted text clipped - 10 lines]
> 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.

Signature
Thomas T. Veldhouse
Key Fingerprint: D281 77A5 63EE 82C5 5E68 00E4 7868 0ADC 4EFB 39F0
Peter Bromberg [C# MVP] - 17 Nov 2006 22:41 GMT
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

Signature
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
> If I have multiple subscribers to a delegate, what keeps track of them?
> Are they out there on the heap?
[quoted text clipped - 12 lines]
>
> Thanks.
Dave Sexton - 17 Nov 2006 23:03 GMT
Hi Peter,
More accurately, it's the MulticastDelegate class, which derives from
Delegate, that provides the invocation list.

Signature
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
[quoted text clipped - 21 lines]
>>
>> Thanks.
William Stacey [C# MVP] - 18 Nov 2006 02:55 GMT
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.

Signature
William Stacey [C# MVP]
| Hi Peter,
|
[quoted text clipped - 26 lines]
| >>
| >> Thanks.
Dale - 19 Nov 2006 20:58 GMT
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

Signature
Dale Preston
MCAD C#
MCSE, MCDBA
> 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.
[quoted text clipped - 31 lines]
> | >>
> | >> Thanks.