!Dan Vallejo's WebLog!

Visual Studio .NET Developer

Events versus Delegates

So what is the difference between events and delegates? The reality is that currently they are basically the same. The difference is that the += operator that is used to add a delegate function to an event is thread-safe whereas when using a delegate the += operator is not thread-safe.

events have always been multi-cast, meaning that an event might call multiple functions. In the beta version of .NET delegates where single-cast meaning that only a single delegate function would be called. But in the RTM version of .NET 1.0, delegates were made multi-cast.

If you look at the MSIL you'll see more differences. But in general, you can simply remove the event keyword and your program for the most part will work the same as it did.

The main usefulness that I see is that it indicates to the user what your intentions are. A delegate is simply a function pointer that can be used to call custom code such as passing in the comparison function. An event is used to notify the user that a special event has happened such as new mail has arrived.

Published Saturday, May 22, 2004 3:16 PM by DanVallejo
Filed under:

Comments

 

Christian said:

I guess the difference is only found in the design.
You should use an event in you application if you want notify somebody (like you said) and a delegate as simple function pointer...
May 23, 2004 3:22 AM
 

Andreas said:

There's another (important) difference. If you make an event public, others can subscribe to it but never invoke it. It's "semi"-public. If you make a delegate public, anyone can invoke it or alter the invokation list.
May 23, 2004 6:53 AM
 

Dumky said:

May 23, 2004 8:53 AM
 

manoj said:

There's another (important) difference. If you make an event public, others can subscribe to it but never invoke it. It's "semi"-public. If you make a delegate public, anyone can invoke it or alter the invokation list.
July 31, 2004 10:42 PM
 

richTextBox e stream | hilpers said:

January 21, 2009 12:42 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker