events like prioperties

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

I am sort of confused here. Jon Skeet allegedly claims that evens are
like properties. To me, on the contrary, they look more like
instances of delegates.

What's the deal here?

Thanks
 
puzzlecracker said:
I am sort of confused here. Jon Skeet allegedly claims that evens are
like properties. To me, on the contrary, they look more like
instances of delegates.

What's the deal here?

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.
 
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
 
Other then being able to use events as properties, is there something
you can do with events that you cannot do with
delegates?

I never said you could use events as properties. I merely said they
were *like* properties in that they basically represent a pair of
methods.

The benefit of using events instead of public delegate variables is
the same as the benefits of using properties instead of public
variables in general - it gives you more control.

With events, only the ability to subscribe/unsubscribe is exposed -
clients can't raise the event, or set the variable to some arbitrary
delegate (effectively removing any other handlers which have
subscribed). Likewise you can also put in your own add/remove logic,
if you want to use a different storage mechanism (e.g.
EventHanderList).

If you haven't read http://pobox.com/~skeet/csharp/events.html I
suggest you do so. You may well have read it already though - I
wouldn't be surprised if it were that article which prompted the
question.

Jon
 
Back
Top