M
muler
Hi all,
I can't figure out why we need the 'event' keyword, because without
having it I can accomplish the same thing. So, why do we need to put
the event keyword?
namespace DelegateEx
{
delegate void BeastCaptured(string msg);
class A
{
// without having the 'event' keyword added this class can
notify other classes.
// so why do we need the event keyword.
// program behavior same if using: public event BeastCaptured
evt;
public BeastCaptured evt;
public void Loop()
{
for (int i = 0; i < 1000; ++i)
if (i == 666 && evt != null)
{
evt("Beast captured!");
break;
}
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
a.evt += delegate(string msg)
{
Console.WriteLine("Event handled: " + msg);
};
a.Loop();
}
}
}
Output:
Event handled: Beast Captured!
Or, is this working because I put both classes on the same assembly?
Your comments highly appreciated,
Muler
I can't figure out why we need the 'event' keyword, because without
having it I can accomplish the same thing. So, why do we need to put
the event keyword?
namespace DelegateEx
{
delegate void BeastCaptured(string msg);
class A
{
// without having the 'event' keyword added this class can
notify other classes.
// so why do we need the event keyword.
// program behavior same if using: public event BeastCaptured
evt;
public BeastCaptured evt;
public void Loop()
{
for (int i = 0; i < 1000; ++i)
if (i == 666 && evt != null)
{
evt("Beast captured!");
break;
}
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
a.evt += delegate(string msg)
{
Console.WriteLine("Event handled: " + msg);
};
a.Loop();
}
}
}
Output:
Event handled: Beast Captured!
Or, is this working because I put both classes on the same assembly?
Your comments highly appreciated,
Muler