473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

events like prioperties

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
Oct 5 '08 #1
5 1193
puzzlecracker <ir*********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 5 '08 #2
On Sun, 05 Oct 2008 13:12:36 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
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?
You're both right.

But, as is usually the case when comparing something Jon says to something
any of the rest of us say, Jon is "more right". :)

In particular, what you are looking at is automatically-generated events.
When you declare an event without an implementation, the compiler provides
the implementation for you. Inside the class in which the event is used,
the event looks like a delegate, because that's exactly what the compiler
provides for you (*).

But, behind the scenes the compiler is also adding methods to support the
event. And literally, an event _always_ consists of exactly two methods:
an "add" method and a "remove" method. The field in which the delegate is
stored is implicitly created by the compiler for auto-generated events,
but it's not a mandatory part of an event; only the "add" and "remove"
methods are (you could explicitly implement an event that managed the
subscription to the event in some way other than a simple delegate field).

So, in the same way that the thing that defines a property is the presence
of the "get" and "set" methods (optionally omitting one or the other), the
thing that defines an event is the presence of the "add" and "remove"
methods (though for events, they are not optional). This is what Jon
means when he says events are like properties.

Pete

(*) There are interesting side-effects to this auto-generated behavior:

-- When you take advantage of the auto-generated event, from inside
the class you do _not_ have access to the implementation of the event.
This is normally not an issue, but it means that even though the default
implementation of an event provides for thread-safe access to the event
field, modifying the event field from within the class in that case could
result in a corrupted event because that doesn't go through the accessors.

-- The compiler is required to use the containing object ("this") when
locking for instance events and the type object for static events. In
both cases, this means that the lock is taken using a public reference,
which is usually considered a bad idea: in general, it means that other
code could be locking on the same object at the same time, increasing the
chance of deadlock, and at a minimum increasing contention for the lock.
Fortunately, for an event there's not any subsequent locking that takes
place within the add and remove methods, and so the event should not in
and of itself be able to lead to deadlocking. But I find it interesting
to note the exception to the rule being implicitly expressed in the C#
spec. :)
Oct 5 '08 #3
On Oct 5, 4:38*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
puzzlecracker <ironsel2...@gmail.comwrote:
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.

--
Jon Skeet - <sk...@pobox.com>
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
Oct 5 '08 #4
On Sun, 05 Oct 2008 14:56:22 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
Other then being able to use events as properties,
Careful with your language there. Events behave similarly to properties,
but you cannot "use events as properties".
is there something
you can do with events that you cannot do with
delegates?
In the same way that properties allow you to abstract away the
implementation from the behavior, so too do events. At a minimum, just
like a property allows you to hide the actual storage, preventing outside
code from accessing it without going through your accessors (which allows
you to do things like lazily instantiate the value, raise an event, update
the UI, log something to disk, validate the value, etc. when the property
is accessed, without worrying that the event might be accessed without
your code being involved).

But also like properties, it allows for a more-complex implementation of
the event itself. For example, the Control class uses a dictionary to
keep track of all the events that have actually been subscribed. There
are so many events in a Control, and so few of them are ever actually
subscribed at any given time, that by not even storing a null reference
for those events that aren't subscribed, a significant memory savings is
had.

Basically, it's an abstraction in the same way that properties are, and
with the same kinds of advantages.

Pete
Oct 5 '08 #5
On Oct 5, 10:56*pm, puzzlecracker <ironsel2...@gmail.comwrote:
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
Oct 6 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vince C. | last post by:
Hi. I'd like to trap ADO Recordset object events in my ASP script (either VBS or JS, no preference). I've tried (in VBS) writing a Sub rs_RecordChangeComplete( adReason, cRecords, pError,...
8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
3
by: Sasha | last post by:
Hi everyone, Here is my problem: I have the following classes: - DataNode - this class is designed to hold some data and will be contained in a tree like data structure DataTree. When...
6
by: Saso Zagoranski | last post by:
Hi! How can I unregister all the events registered to a control? I have seen a piece of code in another thread, which gets all the registered handlers for a specific event. Let's say I have a...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
14
by: xoozlez | last post by:
Hi there, I have a registration form where I like to filter out the past events of 2007. This is the code I am using : strSQL = "SELECT EventID, EventName, EventDateBegin, EventDateEnd,...
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.