Look at the generated code for "event" under ILDASM, and all will be
revealed. :-)

As others have pointed out, the second syntax basically puts an instance of
the delegate type into the public namespace, just as any other public field.

"event" generates a private field named FooChanged, as well as two methods,
add_FooChanged and remove_FooChanged, that are used when the compiler sees
the += from client code, and the necessary metadata to allow other languages
to hook up to FooChanged. (You can customize these add and remove methods,
by the way, using syntax similar to that of properties.)

Ted Neward
Java, .NET, XML Services
Consulting, Teaching, Speaking, Writing
http://www.tedneward.com
 

> -----Original Message-----
> From: Discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED] On Behalf Of Sébastien Lorion
> Sent: Wednesday, February 20, 2008 1:38 PM
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: Re: [ADVANCED-DOTNET] Missing event keyword
> 
> On 2/20/08, Ryan Heath <[EMAIL PROTECTED]> wrote:
> > In your example, anyone can fire the FooChanged.
> >  When declared as an event, only instances of Foo can fire it, while
> >  registering is still supported through += and -=.
> >
> >  // Ryan
> 
> Not only that but users of the class could also directly assign the
> delegate field:
> 
> x.FooChanged = new EventHandler(blah);
> 
> which will replace all previous registered handlers with the new one.
> 
> In short,
> 
> public EventHandler FooChanged;
> 
> is a field named FooChanged of type EventHandler and
> 
> public event EventHandler FooChanged;
> 
> encapsulates that field and protects its access (external code cannot
> raise the event and cannot change the value of the field. More or less
> like a property.
> 
> Sébastien
> 
> ===================================
> This list is hosted by DevelopMentor®  http://www.develop.com
> 
> View archives and manage your subscription(s) at
> http://discuss.develop.com
> 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.516 / Virus Database: 269.20.8/1289 - Release Date:
> 2/20/2008 10:26 AM
> 

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.516 / Virus Database: 269.21.1/1299 - Release Date: 2/26/2008
9:08 AM
 

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to