473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Unregistering a Delegate

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
3 2304
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Les Caudle | last post by:
After removing the .NET Framework (so that I can install the 1.1 Framework), I get the following error msg in the event log: The description for Event ID ( 1071 ) in Source ( ASP.NET 1.0.3705.288...
6
by: Saso Zagoranski | last post by:
Hi! How can I unregister all the events registered to a control? I have seen a piece of code in another thread, which gets all the registered handlers for a specific event. Let's say I have a...
1
by: | last post by:
Hi all I am using RegAsm.exe to register .NET assemblies like this: regasm D:\...\...\MyDLL.dll /tlb:d:\mybin\MyDLL.tlb for unregistering: regasm D:\...\...\MyDLL.dll /tlb:d:\mybin\MyDLL.tlb...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.