Delegates VERSUS Events

G

Guest

I have the following code to tie up a custom delegate (ConsoleDelegate) with
two methods from a separate class called CallMyEvents:

static void Main()
{
CallMyEvents call = new CallMyEvents();
ConsoleDelegate del += new ConsoleDelegate(call.FirstMethod);
del += new ConsoleDelegate(call.SecondMethod);
del();
}

By calling del(), both methods are called in turn.

In the CallMyEvents class I have also declared an event: public event
ConsoleDelegate myEvent;

I have a method to call the event:

public void OnMyEvent()
{
if(myEvent != null)
myEvent();
}

I changed the Main method to the following:

CallMyEvents call = new CallMyEvents();
call.myEvent += new ConsoleDelegate(call.FirstMethod);
call.myEvent += new ConsoleDelegate(call.SecondMethod);
Call.OnMyEvent();

Calling OnMyEvent also calls both methods in turn.

My question is, what is the difference between using just the delegate to
call the methods and tying the delegate to an event?

What are the benefits of using events rather than just delegates and what
restrictions are there if I just go with delegates.

If anyone could give me a simple answer I would be most grateful.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
My question is, what is the difference between using just the delegate to
call the methods and tying the delegate to an event?

What are the benefits of using events rather than just delegates and what
restrictions are there if I just go with delegates.

If anyone could give me a simple answer I would be most grateful.

Events are implemented using delegates , in some cases they are exactly the
same, basically when you have an event that is hooked to one, and only one
handler. in this case it's the same if you define it as an event or as a
delegate.

from the above you may get that the differences being that an event is a
multicast, you can have more than one handlers attached to the same event
and all will be called sequencialy.
How is this done?
Simply by some magic frmo the compiler, when you declare an event the
compiler transform it in a pair of methods , IIRC they are named
add_YourEventName( theDelegateOfTheEvent )
remove_YourEventName( theDelegateOfTheEvent )

so when you use += the first one is used .


IIRC in MSDN magazine there is a good article about that, I found it a time
ago while trying to explain exactly the same :)
 
N

Nick Hounsome

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


Events are implemented using delegates , in some cases they are exactly
the same, basically when you have an event that is hooked to one, and only
one handler. in this case it's the same if you define it as an event or as
a delegate.

No it isn't - they have a completely different set of available operations
(from ouotside the class).
from the above you may get that the differences being that an event is a
multicast, you can have more than one handlers attached to the same event
and all will be called sequencialy.

Actually all delegates are multicast too. The terminology is most confusing
but "delegate void D();" creates a new class derived from MulticastDelegate.
 
I

Ignacio Machin \( .NET/ C# MVP \)

HI,

Nick Hounsome said:
No it isn't - they have a completely different set of available operations
(from ouotside the class).

You are correct, the operations are not the same.
Actually all delegates are multicast too. The terminology is most
confusing but "delegate void D();" creates a new class derived from
MulticastDelegate.

I was under the impression that the delegate inherit from Delegate ( at
least in 1.1)
 
J

Jon Skeet [C# MVP]

<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
I was under the impression that the delegate inherit from Delegate ( at
least in 1.1)

Nope - I think they did in an early beta of 1.0, but it was decided
pretty soon that MulticastDelegate was much more useful. Certainly the
1.1 compiler creates types derived from MulticastDelegate.

The C# spec talks about it deriving from Delegate, but I suppose they
could say it derives indirectly from Delegate anyway...
 
I

Ignacio Machin \( .NET/ C# MVP \)

HI,

Nope - I think they did in an early beta of 1.0, but it was decided
pretty soon that MulticastDelegate was much more useful. Certainly the
1.1 compiler creates types derived from MulticastDelegate.

You are right, MSDN says it also, no idea where I got from the Delegate
part
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top