all groups > c# > june 2004 >
You're in the

c#

group:

Question about Events and delegates


Re: Question about Events and delegates Daniel O'Connell [C# MVP]
6/3/2004 4:51:19 PM
c#:

[quoted text, click to view]

The event keyword creates an event property(literally, event results in two
methods, add_EventName and remove_EventName, which are used to add and
remove listener delegates. The spec also defines a protected raise_EvetnName
accessor, but C# nor VB uses it to my knowledge, C++ does I think).
Basically it hides the delegate from outsiders. It restricts raising the
event to your local class and keeps others from modifying(or accessing) the
delegate list, that combined with the += and -= C# syntax and metadata
declaring the member is an event, not just a delegate are the primary
reasons. Imagine the fun if external code could wipe out all listeners or
fake events being raised.

I'd never recommend using direct delegate fields.
[quoted text, click to view]

Re: Question about Events and delegates Daniel O'Connell [C# MVP]
6/3/2004 5:22:52 PM

[quoted text, click to view]
Well, += and -= for C# and events is a bit of syntactic sugar to the add and
remove accessors.

[quoted text, click to view]

Re: Question about Events and delegates Daniel O'Connell [C# MVP]
6/3/2004 5:23:05 PM

[quoted text, click to view]

Keep forgetting about the FAQ...
[quoted text, click to view]

Re: Question about Events and delegates Jon Skeet [C# MVP]
6/3/2004 11:10:13 PM
[quoted text, click to view]

<snip>

[quoted text, click to view]

See http://www.pobox.com/~skeet/csharp/faq/#event.delegate

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Question about Events and delegates Flare
6/3/2004 11:42:37 PM
Hi i have a qusstion about events and delegates. Especially the precis role
of the Event.

Eg. We have a class wich want to fire events so we declare:

public delegate void TestEventHandler(object sender, EventArgs arg);
public event TestEventHandler Test;

And fire the event with

if(Test != null)
Test(this, new EventArg());

Is the right:
Test is actually a MultiCast delgate? Debugger says it is! OK.

Buth then why use the event keyword in front of the Test and insted not
just:

public delegate void TestEventHandler(object sender, EventArgs arg);
public TestEventHandler Test;

And fire the event with
if(Test != null)
Test(this, new EventArg());

It work fine. Why do we need the event keyword? Clearly there is a reason.
Someone who can tell me?

reagards
Anders

Re: Question about Events and delegates Flare
6/4/2004 12:11:56 AM
[quoted text, click to view]

Ok. So. Event dosent provide "extra functionality" except the delagate
hideing (good point). Right?

About the add and remove wich you say isnt implemented in C#...isnt it used
in the overloaded syntax += on the delegate?

Actually thougt that the event had more "logic".

Anders

Re: Question about Events and delegates Flare
6/4/2004 12:29:40 AM
[quoted text, click to view]

Hehe. And the link in the FAQ to the MSDN article cleard up the rest. Thanks
to all of you.

Regards
Anders J

AddThis Social Bookmark Button