Re: Where is the delegate?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Nick Hounsome wrote:

<snip>

this.btnAccept.Click += new System.EventHandler(this.btnAccept_Click);

The delegate is Click.

No, that's the event. There's no guarantee that it's backed by a simple
delegate. Heck, it might not even be backed by a delegate at all
(although it would be an odd implementation which didn't have a
delegate somewhere).

The only delegate in the above statement is
new EventHandler (this.btnAccept_Click)

The only difference between
"public event EventHandler Click;" and "public EventHandler Click;"
is the operations that you are allowed to perform on the delegate from
outside the class.

No, that's not true. The first declares both a public event (with some
default access methods) *and* a private delegate field. The second
declares just a public delegate field. While the broad result is as you
say, there are other differences some of which (as you said later) you
can detect with reflection. Here's another difference which you can't
detect by reflection though - the first form will lock the monitor
associated with the object it's called on for the duration off
add/remove operations. (In C# 2.0 it could be a different monitor; in
C# 1.1 it's always the containing object or the type for a static
event.)

While events and delegates often look the same, I don't think it's a
good idea to trivialise the differences between them. The difference
between an event and a delegate field is similar to the difference
between a property and a "normal" field. The operations involved
(add/remove for events, set/get for properties) are often implemented
as "straight-through" operations on a field, but they certainly don't
have to be.

<snip>

Jon

.



Relevant Pages

  • Re: Matching Event Handlers in an Interface
    ... map handlers in the receiver with events in the sender, ... This shows how to create a delegate using information in the EventInfo ... > For example in VB.NET the Changed Event has a ChangeEvent delegate field ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Where is the delegate?
    ... The delegate is Click. ... default access methods) *and* a private delegate field. ... detect by reflection though - the first form will lock the monitor ... add/remove operations. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Where is the delegate?
    ... it might not even be backed by a delegate at all ... default access methods) *and* a private delegate field. ... detect by reflection though - the first form will lock the monitor ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Clarifying the Relationship Between Delegates and Events
    ... Well, they're really very different, but the "field-like events" of C# ... delegate field, you can set/fetch/execute that delegate; ... a delegate field of the same name. ... The property itself is just a pair of methods, not a storage location. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Detecting EventHandler
    ... You cannot check an event for handlers. ... you have back-up delegate field (the default case when you declare an ... "Eddy POULLET" wrote in message ... > Delegate[] DelegateList = ((MulticastDelegate ...
    (microsoft.public.dotnet.languages.csharp)