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

Unregistering a Delegate

P: n/a
Please consider this sample code: It registers a delegate with an event.

p1.FirstNameChanged += new Person.NameChanged(p1_FirstNameChanged);

Now the following code removes the delegate:
p1.FirstNameChanged -= new Person.NameChanged(p1_FirstNameChanged);

The above line that removes the delegate confuses me. It works - buy why
must we use the 'new' keyword? The delegate already exists and is
"registered" with the event. I would think that we would need to somehow get
a reference to an existing delegate or somehow tell the event to remove an
*existing* delegate. It seems to me that the use of the 'new' keyword would
necessarily create a new delegate, and could not possibly obtain any
reference an existing delegate. Yet it works. I'm totally confused.

Thanks!
Feb 27 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jeff,
It seems to me that the use of the 'new' keyword would
necessarily create a new delegate, and could not possibly obtain any
reference an existing delegate. Yet it works. I'm totally confused.


The only thing that matters is that the two delegates represent the
same methods (and the same instance if the method is non static). In
other words, that delegate1.Equals(delegate2) == true.

If you find it less confusing you can of course use the same delegate,
but then you have to keep a reference to it somewhere.

Person.NameChanged x = new Person.NameChanged(p1_FirstNameChanged);

p1.FirstNameChanged += x;
....
p1.FirstNameChanged -= x;
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 27 '06 #2

P: n/a
Dnia 27-02-2006 o 22:57:49 Jeff S <A@B.COM> napisa³:
Please consider this sample code: It registers a delegate with an event.

p1.FirstNameChanged += new Person.NameChanged(p1_FirstNameChanged);

Now the following code removes the delegate:
p1.FirstNameChanged -= new Person.NameChanged(p1_FirstNameChanged);

The above line that removes the delegate confuses me. It works - buy why
must we use the 'new' keyword? The delegate already exists and is
"registered" with the event. I would think that we would need to somehow
get
a reference to an existing delegate or somehow tell the event to remove
an
*existing* delegate. It seems to me that the use of the 'new' keyword
would
necessarily create a new delegate, and could not possibly obtain any
reference an existing delegate. Yet it works. I'm totally confused.

[PD] Please note that events are internally converted to private
MulticastDelegate field and two method (this are the methods that are
called when you use operators += and -=). When you call -= the
Delegate.Remove method is used on the private field created by the
compilator. This method removes from the invocation list delegate that is
equal to the parameter. According to documentation delegates are equal
when they "have the same targets, methods, and invocation list". When you
create the second delegate pointing to the same method of the same object
it is equal to the delegate you created and added earlier, so it gets
removed from the event invocation list. Hope I have put it clear enough :)

--
Piotr Dobrowolski
Piotr.Dobrowolski@_usun_gmail.com
Feb 27 '06 #3

P: n/a
Okay, I'll buy it... and yes, your explanation actually makes sense. So even
though it's a new "runtime delegate object" per the 'new' keyword, it's the
..NET Framework's implementation of the -= operator, as implemented for
delegates, that ensures that any [already existing] delegates get removed
from the event (really from the invocation list).

Thank you.

"Piotr Dobrowolski" <Piotr.Dobrowolski@_usun_gmail.com> wrote in message
news:op.s5nlnfm7zowjqs@da-pdobrowolski...
Dnia 27-02-2006 o 22:57:49 Jeff S <A@B.COM> napisa³:
Please consider this sample code: It registers a delegate with an event.

p1.FirstNameChanged += new Person.NameChanged(p1_FirstNameChanged);

Now the following code removes the delegate:
p1.FirstNameChanged -= new Person.NameChanged(p1_FirstNameChanged);

The above line that removes the delegate confuses me. It works - buy why
must we use the 'new' keyword? The delegate already exists and is
"registered" with the event. I would think that we would need to somehow
get
a reference to an existing delegate or somehow tell the event to remove
an
*existing* delegate. It seems to me that the use of the 'new' keyword
would
necessarily create a new delegate, and could not possibly obtain any
reference an existing delegate. Yet it works. I'm totally confused.

[PD] Please note that events are internally converted to private
MulticastDelegate field and two method (this are the methods that are
called when you use operators += and -=). When you call -= the
Delegate.Remove method is used on the private field created by the
compilator. This method removes from the invocation list delegate that is
equal to the parameter. According to documentation delegates are equal
when they "have the same targets, methods, and invocation list". When you
create the second delegate pointing to the same method of the same object
it is equal to the delegate you created and added earlier, so it gets
removed from the event invocation list. Hope I have put it clear enough :)

--
Piotr Dobrowolski
Piotr.Dobrowolski@_usun_gmail.com

Feb 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.