473,325 Members | 2,792 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,325 software developers and data experts.

Events and Delegates

I've read that events are a C# implementation of the Observer design pattern
which states that:

There is a source object providing an event, subscriber objects can
subscribe to the event and the the source object is responsible for
notifying all events, with 2 parameters part of the event, source object and
event arguments.

Typically in C# this is how it's done:

public delgate void ClickEventHandler(object source, EventArgs e);

class Source
{
// class implementation
public event ClickEventHandler Click;

public void SomeMethod()
{
//... processing
Click(this, new EventArgs()); // raise the event
}
}

public class Subscriber
{
private Source src = new Source();

public void Init()
{
src.Click += new ClickEventHandler(ClickHandler);
}
public void ClickHandler(object source, EventArgs e)
{
// procsessing
}
}

the way I understand it, the C# compiler emits add & remove methods to the
Source class to support the += and -= operators, which simplify the
subscribing of events, or more exactly, combining of MulticastDelegate
objects. The event is simply a MuliicastDelegate object and I could have
done this:

public delgate void ClickEventHandler(object source, EventArgs e);

class Source
{
// class implementation
public ClickEventHandler Click = new ClickEventHandler;

public void SomeMethod()
{
//... processing
Click(this, new EventArgs()); // raise the event
}
}

public class Subscriber
{
private Source src = new Source();

public void Init()
{
ClickEventHandler del = new ClickEventHandler(ClickHandler);
src.Click = del;
}
public void ClickHandler(object source, EventArgs e)
{
// procsessing
}
}

so in the end, the C# event mechanism provides a more efficient syntactical
construct for subscribing to an event then what I did using just
MulticastDelegate members. Please let me know where I'm wrong.

P.S. I know that you can override the .add and .remove handlers for an event
declaration to provide custom processing, which is an addition advantage of
using event keyword.
Nov 16 '05 #1
1 2332

"Manco" <ma***@dollars.net> wrote in message
news:pyssd.7316$PL1.4361@trnddc09...
I've read that events are a C# implementation of the Observer design
pattern which states that:
I wouldn't say they are an implementation of the Observer pattern, but they
do provide a pretty clean method of implementing it, IMHO.

Typically in C# this is how it's done:

public delgate void ClickEventHandler(object source, EventArgs e);

class Source
{
// class implementation
public event ClickEventHandler Click;

public void SomeMethod()
{
//... processing
Click(this, new EventArgs()); // raise the event
}
}

public class Subscriber
{
private Source src = new Source();

public void Init()
{
src.Click += new ClickEventHandler(ClickHandler);
}
public void ClickHandler(object source, EventArgs e)
{
// procsessing
}
}

the way I understand it, the C# compiler emits add & remove methods to the
Source class to support the += and -= operators, which simplify the
subscribing of events, or more exactly, combining of MulticastDelegate
objects. The event is simply a MuliicastDelegate object and I could have
done this:

The event is not a MulticastDelegate object anymore than a property is a
string object. The event is the set of methods that provide an interface
access the event implementation and some associated metadata. The event in
and of itself does not actually provide any delegate, the implementation
must do that. The C# compiler just hides that from you in many cases.

so in the end, the C# event mechanism provides a more efficient
syntactical construct for subscribing to an event then what I did using
just MulticastDelegate members. Please let me know where I'm wrong.


for this to be true, your assignment would have to be:

src.Click = (ClickEventHandler)Delegate.Combine(src.Click,del) ;
Nov 16 '05 #2

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

Similar topics

4
by: Marty McDonald | last post by:
It is still unclear to me why we would use events when delegates seem to do just fine. People say that events make it so the publisher doesn't need to know about the listeners. What does that...
7
by: Rakesh Rajan | last post by:
Hi, I find that when i define a delegate, it gets derived from MulticastDelegate, which provides all funtionality that events would provide (like registering new handlers etc.). Then, apart from...
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...
3
by: Chris | last post by:
Hi, what is the difference between using events and delegates (apart from the syntax) ? have a look at following (working) programs please (you can just copy/paste and build it) : First...
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...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.