all groups > dotnet clr > july 2005 >
You're in the

dotnet clr

group:

delegates vs events



Re: delegates vs events Atul
7/22/2005 12:00:00 AM
dotnet clr: Also, an event will have designer support, meaning if your class can be used
at design-time, VS.Net will allow developers to subscribe to the event using
the Forms designer.

-Atul
http://www.ssware.com/
Shell MegaPack - Windows Explorer Shell Controls for ActiveX and .Net


"Alexander Kolev" <AlexanderKolev@discussions.microsoft.com> wrote in
message news:65BA2F05-7EAC-4DD1-980C-1A0DB06B5A41@microsoft.com...
[quoted text, click to view]

delegates vs events Alexander Kolev
7/22/2005 12:18:01 AM
I've a general question.Given the source code below, which produces the same
result what's the difference between delegates and events ? When to use event
and when delegate ?
class Test1
{
public event Del EvType;
public Del DelType;

public void fun1()
{
Console.WriteLine( "fun1 inviked" );
}
public void fun2()
{
Console.WriteLine( "fun2 inviked" );
}
public void OnTest()
{
EvType += new Del(fun1);
EvType += new Del(fun2);

DelType += new Del( fun1 );
DelType += new Del( fun2 );

Console.WriteLine( "call EvType" );
EvType();

Console.WriteLine( "call DelType" );
DelType();
}
static void Main(string[] args)
{
new Test1().OnTest();
}
RE: delegates vs events Rahul Anand
7/22/2005 1:44:02 AM
[quoted text, click to view]

Hi Alexander,

Event (as defined under MSDN) :
The event keyword lets you specify a delegate that will be called upon the
occurrence of some "event" in your code.

Basically we use delegates to hold reference to methods and we expose a
public event so that the interested parties can register their interest in
the exposed event.

You can achieve all this also by directly exposing the delegate to outside
world. And listeners can use it to attach or detach their methods. But this
is a standard and cleaner approach to use delegate, event, and protected
OnEvent method together, when you are providing the functionality of event
raising (in your class) and handling (in listner classes).

Hope it will help.

--
Cheers,
Re: delegates vs events Jon Shemitz
7/22/2005 9:07:47 AM
[quoted text, click to view]

An event can only be a class member; not a local variable.

--

Re: delegates vs events Jon Skeet [C# MVP]
7/22/2005 5:44:43 PM
[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
AddThis Social Bookmark Button