Home All Groups Group Topic Archive Search About
Author
10 Mar 2005 11:41 AM
Amir Shitrit
What's the different between an event and a delegate?
Author
10 Mar 2005 1:04 PM
thomas woelfer
Amir,

basically, an event is a more limited version of a delegate.

more details here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp04192001.asp

WM_HOPETHISHELPS
thomas woelfer


Show quoteHide quote
"Amir Shitrit" <AmirShit***@discussions.microsoft.com> schrieb im Newsbeitrag
news:EB40D3E3-0B7C-4DC4-8C9F-3653267779CD@microsoft.com...
> What's the different between an event and a delegate?
Are all your drivers up to date? click for free checkup

Author
10 Mar 2005 7:39 PM
Marcos Stefanakopolus
Well, kind of.  Events are methods of a class that are associated with some
delegate type, but are marked with the "event" keyword so that the compiler
knows what to do with them.  "event" in C# is really nothing more than an
access modifier like "private", "public", or "sealed".  The specific level
of access granted by the "event" keyword depends on whether code that's
referencing that method is part of the class that defined the method, or
outside of the class.  Code that is part of the class is allowed to call the
method (and thereby, "raise" the event), whereas code outside of the class
is only allowed to subscribe to the event by using the += operator to add an
appropriate delegate method to the event method's list of subscribers.  For
instance, if class A has an event called "Fire", then class A is allowed to
do this:

// Declare a type signature for the Fire event:
public delegate void AFireType(object sender, EventArgs e);
// create an instance of the AFireType delegate and mark it as an event:
public event AFireType Fire;

// elsewhere...
if(temperature >= (Farenheit)415.0) {
    if(Fire != null)
        Fire()
}

Class B, however, can't do anything more than this:

// define a handler for class A's Fire event:
public void OnFire(object sender, EventArgs e) { // what to do if something
is on fire };

// Create an A object and subscribe to it's Fire event:
A MyA = new A();
MyA.Fire += new A.AFireType(OnFire)



Show quoteHide quote
"thomas woelfer" <passpo***@woelfer.com> wrote in message
news:ewYQ6GXJFHA.2640@TK2MSFTNGP09.phx.gbl...
> Amir,
>
> basically, an event is a more limited version of a delegate.
>
> more details here:
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp04192001.asp
>
> WM_HOPETHISHELPS
> thomas woelfer
>
>
> "Amir Shitrit" <AmirShit***@discussions.microsoft.com> schrieb im
> Newsbeitrag news:EB40D3E3-0B7C-4DC4-8C9F-3653267779CD@microsoft.com...
>> What's the different between an event and a delegate?
>
>
Author
13 Mar 2005 9:50 AM
Jon Skeet [C# MVP]
Amir Shitrit <AmirShit***@discussions.microsoft.com> wrote:
> What's the different between an event and a delegate?

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Bookmark and Share