Re: Where is the delegate?
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: 6 Mar 2006 01:26:10 -0800
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
.
- Follow-Ups:
- Re: Where is the delegate?
- From: Nick Hounsome
- Re: Where is the delegate?
- References:
- Re: Where is the delegate?
- From: Nick Hounsome
- Re: Where is the delegate?
- Prev by Date: Re: Problem with directoryServices and authentication
- Next by Date: Re: Problem with directoryServices and authentication
- Previous by thread: Re: Where is the delegate?
- Next by thread: Re: Where is the delegate?
- Index(es):
Relevant Pages
|