Events are multicast and delegates are single-cast. A delegate is
basically a variable containing a reference to one method, while one
event may have several subscribers.
I surmise that you only need one callback, so the event represents an
unneccesary overhead. Probably the delegate will be faster, although
not by much. An even faster method is by using an interface like this:
public interface IMyCallback {
void NotifyUpdate(MyCallbackArgs args);
}
public class MyClass {
IMyCallback listener;
public MyClass(IMyCallback listener) {
this.listener = listener;
}
public void Run() {
while(true) {
DoALotOfWork();
listener.NotifyUpdate(new MyCallbackArgs());
}
}
}
It has a distinct java feel to it but it will be a bit faster than
using a delegate. However, architecturally a delegate seems to make
more sense and the overhead isn't that large anyway so probably
delegates are the way to go.
Regards,
Bram
> Events are multicast and delegates are single-cast.
No, that's not true at all.
An event is fundamentally just a pair of methods - add and subscribe.
They've got to use delegates to do their job properly. Delegates are
inherently multicast. (At one stage MS was going to differentiate
between single-cast and multicast, hence Delegate and
MulticastDelegate, but they decided against it in the end.)
Here's a short but complete example demonstrating this - no events are
used anywhere, but it's clearly multicast behaviour.
using System;
class Test
{
static void Main()
{
Action<string> x = FirstMethod;
x += SecondMethod;
x ("Test");
}
static void FirstMethod(string s)
{
Console.WriteLine ("First: {0}", s);
}
static void SecondMethod(string s)
{
Console.WriteLine ("Second: {0}", s);
}
}

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Bram - 20 Jul 2007 11:13 GMT
Hi Jon!
Mea culpa, thanks for correcting me. But now I'm not sure what the
difference between a delegate and an event is. What is the difference
between the delegate and the event in the following piece of code?
public delegate void MyHandyIntFunction(int p);
public MyHandyIntFunction myDelegate;
public event MyHandyIntFunction myEvent;
Both support the += and -= operators and both are multicast (contrary
to my previous belief).
Regards
Bram Fokke
> > Events are multicast and delegates are single-cast.
>
[quoted text clipped - 36 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 20 Jul 2007 14:45 GMT
> Mea culpa, thanks for correcting me. But now I'm not sure what the
> difference between a delegate and an event is. What is the difference
[quoted text clipped - 5 lines]
>
> public event MyHandyIntFunction myEvent;
There are details at http://pobox.com/~skeet/csharp/events.html
Jon