They're like properties in that the events themselves don't allocate
any storage - they're just the add/remove pair of methods, just like
properties are the get/set pair of methods. However, also like
properties, events tend to have a variable backing the event. C# allows
a simplified syntax of:
public event EventHandler Foo;
which declares both an event *and* a variable - within the class, using
"Foo" refers to the variable; outside the class it refers to the event.
Outside the class there is still no direct access to the variable
though.
--
Jon Skeet - <
[email protected]>
Web site:
http://www.pobox.com/~skeet
Blog:
http://www.msmvps.com/jon.skeet
C# in Depth:
http://csharpindepth.com
Other then being able to use events as properties, is there something
you can do with events that you cannot do with
delegates?
Take a look here:
delegate string MyDelegateHandler(int x);
class ServicingToEvents{
public event MyDelegateHandler handler;
onMyDelegateHandler(int x){
if(handler!-null)
handler(x);
}
protected void DispatchEvents()
{
int i;
//read
onMyDelegateHandler(i);
}
}
class ServicingToDelegates{
public MyDelegateHandler handler;
onMyDelegateHandler(int x){
if(handler!-null)
handler(x);
}
protected void DispatchEvents()
{
int i;
//read
onMyDelegateHandler(i);
}
}
class EntryPoint{
{
static void Main(string[] args)
{
ServicingToEvents ste=new ServicingToEvents();
ste.handler += MyHandler;
ServicingToDelegates std=new ServicingToDelegates();
ste.handler=new MyDelegateHandler(MyHandler);
}
public static MyHandler(int x)
{
System.WriteLine("{0} is received")
}
}
I don't see the clear difference between these two beasts, other than
events give more typing convenience.
Please explain.
Thanks