How is "myButton.Click -= new EventHandler(myButton_Click)"implemented?

  • Thread starter cannedluncheonmeatobfuscator
  • Start date
C

cannedluncheonmeatobfuscator

After confirming that it really is appropriate to use the "new"
keyword to remove an event handler, as in:

myButton.Click -= new EventHandler(myButton_Click);

I have to ask... how does this compile? I've seen code samples that
store a reference to the event handler in a member variable, then use
the member variable to remove the handler from the event:

myButton.Click -= myHandler;

That seems much more intuitive, so I'm wondering what I get by using
the "new" keyword instead. Are C# delegates inherently pseudo-
singleton (one instance per target method)?

Thanks,

cs
 
P

Pavel Minaev

After confirming that it really is appropriate to use the "new"
keyword to remove an event handler, as in:

    myButton.Click -= new EventHandler(myButton_Click);

This is the original syntax introduced in C# 1.0. Most likely, its
main purpose is to distinguish between various overloads in case of
overloaded methods (since explicitly naming the delegate type
precisely identifies the signature, and thus the exact method that is
needed).
I have to ask... how does this compile? I've seen code samples that
store a reference to the event handler in a member variable, then use
the member variable to remove the handler from the event:

    myButton.Click -= myHandler;

This is the "new" syntax available from C# 2.0 onwards. Most people
use that.
That seems much more intuitive, so I'm wondering what I get by using
the "new" keyword instead.

As explained earlier, though you can achieve the same thing
(disambiguating overloaded methods) via a cast. Otherwise, semantics
are exactly the same.
Are C# delegates inherently pseudo-
singleton (one instance per target method)?

No. A delegate is just an object of a reference class like any other.
Its members are an object reference, and a raw function pointer -
IIRC, 16 bytes in all on x86 (don't forget vtable, and the monitor
lock). There is a bit of a CLR magic there that allows to create
delegates from raw function pointers freshly obtained directly from a
method token in safe code, but that's about it (I suggest making a
simple C# project that declares a delegate type, creates an instance
of it, and makes a call through that instance, then decompiling it
using ildasm and looking at it to see how it all works).

Whether you do use "new" or not, a new delegate object is created
every time. However, when it comes to comparison (including for the
purposes of -=), two delegates compare equal if they are of the same
type, and reference the same method of the same object. For multicast
delegates, they're equal if every delegate on their invocation list is
equal, in order.
 
B

Ben Voigt [C++ MVP]

Pavel Minaev said:
This is the original syntax introduced in C# 1.0. Most likely, its
main purpose is to distinguish between various overloads in case of
overloaded methods (since explicitly naming the delegate type
precisely identifies the signature, and thus the exact method that is
needed).


This is the "new" syntax available from C# 2.0 onwards. Most people
use that.

The C# 2.0 syntax would be:

myButton.Click -= myButton_Click;

using the name of the method, not the name of a delegate variable.

As Pavel explained, the reason you don't need to do:

EventHandler myHandler = myButton_Click;
myButton.Click += myHandler;
....
....
myButton.Click -= myHandler;

is because the event remove callback function uses semantic equality
comparing the invocation list, not reference equality.
 

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