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;
???
... 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;
???