delegate vs event

G

Guest

hi there this may seem like a silly question but what exactly is the difference
... ok .. that question is probably a bit ambiguous.. i know what delegates
are i know what events are, i use them everyday but..

what are events needed? don't delegates do everything?

look at this code normally i would use an event but i've just stuck with
delegate in this instance and it still works! i'm baffelled!

namespace DelegateTst
{
delegate void ShowName();
class Program
{
ShowName m_show = null;

void Run()
{
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
m_show += p1.ShowName;
m_show += p2.ShowName;
m_show += p3.ShowName;
m_show();
}

static void Main(string[] args)
{
Program prog = new Program();
prog.Run();
}
}

class Person
{
internal void ShowName()
{
Console.WriteLine(GetHashCode().ToString());
}
}
}

Can anyone eplain why it would be better/more efficient/whatever to use an
event
i.e.
change
ShowName m_show = null;
to
event ShowName m_show = null;
???
 
J

Jay B. Harlow [MVP - Outlook]

Brian,
In addition to the comments at:

http://blogs.msdn.com/danvallejo/archive/2004/05/22/139616.aspx

The way I view it is that an Event encapsulates a Delegate Field, similar to
how a Property encapsulates other Fields.

You can provide specific handling for the Event Add & Remove methods,
similar to how you can define custom Property Get & Set methods.

http://msdn.microsoft.com/library/d.../en-us/csspec/html/vclrfcsharpspec_10_7_2.asp


FWIW: VB 2005 (.NET 2.0) allows you to customize the handling of the Add,
Remove & Raise methods for an Event.

http://msdn2.microsoft.com/library/yt1k2w4e(en-us,vs.80).aspx

I have not seen if C# gains an Event.Raise method or not in .NET 2.0.

Hope this helps
Jay

| it's ok, i found my answer, they are basically the same
| http://blogs.msdn.com/danvallejo/archive/2004/05/22/139616.asp
|
| "Brian Keating" wrote:
|
| > hi there this may seem like a silly question but what exactly is the
difference
| > .. ok .. that question is probably a bit ambiguous.. i know what
delegates
| > are i know what events are, i use them everyday but..
| >
| > what are events needed? don't delegates do everything?
| >
| > look at this code normally i would use an event but i've just stuck with
| > delegate in this instance and it still works! i'm baffelled!
| >
| > namespace DelegateTst
| > {
| > delegate void ShowName();
| > class Program
| > {
| > ShowName m_show = null;
| >
| > void Run()
| > {
| > Person p1 = new Person();
| > Person p2 = new Person();
| > Person p3 = new Person();
| > m_show += p1.ShowName;
| > m_show += p2.ShowName;
| > m_show += p3.ShowName;
| > m_show();
| > }
| >
| > static void Main(string[] args)
| > {
| > Program prog = new Program();
| > prog.Run();
| > }
| > }
| >
| > class Person
| > {
| > internal void ShowName()
| > {
| > Console.WriteLine(GetHashCode().ToString());
| > }
| > }
| > }
| >
| > Can anyone eplain why it would be better/more efficient/whatever to use
an
| > event
| > i.e.
| > change
| > ShowName m_show = null;
| > to
| > event ShowName m_show = null;
| > ???
 
I

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

Hi,

I think that one of the comments from the blog gives an important
difference:

There's another (important) difference. If you make an event public, others
can subscribe to it but never invoke it. It's "semi"-public. If you make a
delegate public, anyone can invoke it or alter the invokation list.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Jay B. Harlow said:
Brian,
In addition to the comments at:

http://blogs.msdn.com/danvallejo/archive/2004/05/22/139616.aspx

The way I view it is that an Event encapsulates a Delegate Field, similar
to
how a Property encapsulates other Fields.

You can provide specific handling for the Event Add & Remove methods,
similar to how you can define custom Property Get & Set methods.

http://msdn.microsoft.com/library/d.../en-us/csspec/html/vclrfcsharpspec_10_7_2.asp


FWIW: VB 2005 (.NET 2.0) allows you to customize the handling of the Add,
Remove & Raise methods for an Event.

http://msdn2.microsoft.com/library/yt1k2w4e(en-us,vs.80).aspx

I have not seen if C# gains an Event.Raise method or not in .NET 2.0.

Hope this helps
Jay

| it's ok, i found my answer, they are basically the same
| http://blogs.msdn.com/danvallejo/archive/2004/05/22/139616.asp
|
| "Brian Keating" wrote:
|
| > hi there this may seem like a silly question but what exactly is the
difference
| > .. ok .. that question is probably a bit ambiguous.. i know what
delegates
| > are i know what events are, i use them everyday but..
| >
| > what are events needed? don't delegates do everything?
| >
| > look at this code normally i would use an event but i've just stuck
with
| > delegate in this instance and it still works! i'm baffelled!
| >
| > namespace DelegateTst
| > {
| > delegate void ShowName();
| > class Program
| > {
| > ShowName m_show = null;
| >
| > void Run()
| > {
| > Person p1 = new Person();
| > Person p2 = new Person();
| > Person p3 = new Person();
| > m_show += p1.ShowName;
| > m_show += p2.ShowName;
| > m_show += p3.ShowName;
| > m_show();
| > }
| >
| > static void Main(string[] args)
| > {
| > Program prog = new Program();
| > prog.Run();
| > }
| > }
| >
| > class Person
| > {
| > internal void ShowName()
| > {
| > Console.WriteLine(GetHashCode().ToString());
| > }
| > }
| > }
| >
| > Can anyone eplain why it would be better/more efficient/whatever to
use
an
| > event
| > i.e.
| > change
| > ShowName m_show = null;
| > to
| > event ShowName m_show = null;
| > ???
 
C

cody

Additionally an event cannot have the following modifiers:
readonly,const,volatile because they would make no sense.
 

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