C# Events and Delegates
Difference between C# events and delegates
Recently i was wondering about the difference between C# delegates and events. For me events seemed to be a very thin wrapper around delegates. I have been trying to find out the advantage that the keyword "event" adds. I have to explore more on this aspect. But here i present a simple example containing a publisher, a subscriber, and an event broker.
using System;
namespace Test
{
class Publisher
{
private EventBroker evtBroker;
public void RegisterWithBroker(EventBroker evtBroker)
{
this.evtBroker = evtBroker;
}
public void Publish(string message)
{
this.evtBroker.Notify(message); // <---- statement 1
//this.evtBroker.handler(message); // <---- statement 2
}
}
class EventBroker
{
public delegate void onEvent(string message);
public event onEvent handler; //<---- statement 3
//public onEvent handler; //<--- statement4-a simple delegate
//will be used by puiblishers
public void Notify(string message)
{
handler(message);
}
// //this call is used to subscribe
public void RegisterSubscriber(onEvent handler)
{
this.handler += handler;
}
}
class Subscriber
{
public void ReceivedMessage(string message)
{
Console.WriteLine("Received Message: "+message);
}
}
class Test
{
[STAThread]
static void Main(string[] args)
{
EventBroker broker = new EventBroker();
Subscriber sub1 = new Subscriber();
Subscriber sub2 = new Subscriber();
broker.RegisterSubscriber(new EventBroker.onEvent(sub1.ReceivedMessage));
broker.RegisterSubscriber(new EventBroker.onEvent(sub2.ReceivedMessage));
Publisher pub = new Publisher();
pub.RegisterWithBroker(broker);
pub.Publish("Hello to all subscribers");
}
}
}
The important point to note here is that if you comment out 'statement1' and un-comment 'statement2'. Then you would get a complier error that the event handler can be invoked (raised) only inside the 'EventBroker' which defines it. 'Statement 4' defines the delegate, now if i comment out statement 1 and 3 and un-comment statement 2 and 4. The code works perfectly.
The event i believe has an extra constrain that it can only be invoked from within the class which defines it. Hence the class defining an event should define a method like "Notify" to facilitate other classes raise the event. I believe there are other differences...i just hit upon this one..will look for others...

14 Comments:
IMO an event is a general design concept - typically used for asynchronous communication. Eg the publisher subscriber that you've shown, or seperation of UI and backend procesing - a simple eg could be that a scroll bar becomes smaller and smaller since it subscribes to the event of the main window of the browser receiving larger and larger amount of data. And since it is asynchronous, we're able to scroll around even though the entire page hasnt loaded yet. Anotehr subscriber could be the status bar updates the status.
A delegate (function pointer) is a programming construct. In a way one could say an event is a wrapper, but to be precise a delegate is used to implement the concept of an event.
...so I find the event keyword a bit confusing myself... will check this page later to see you've found out :-)
Well done!
[url=http://bezxcwwd.com/mudf/bhnq.html]My homepage[/url] | [url=http://uqvguopn.com/wzkk/jlpn.html]Cool site[/url]
Good design!
My homepage | Please visit
Great work!
http://bezxcwwd.com/mudf/bhnq.html | http://aqgcwcik.com/xdbf/hzsh.html
Great article! Thanks.
Thanks for interesting article.
Nice! Nice site! Good resources here. I will bookmark!
I see first time your site guys. I like you :)
Excellent website. Good work. Very useful. I will bookmark!
The 'event' keyword is a *modifier* for a delegate field.
The Biggest difference is that you can declare an event in an interface, whereas you can not declare a field in an interface.
This is the best description of the differences that I found: http://blog.monstuff.com/archives/000040.html
You said you will love me wow gold the whole life, but you marry her. You said Cheap WoW Gold you will wow power leveling,come to marry me, but this will not be carried out forever.WoW Gold I am trying my best to forget you and do not love you anymore. wow leveling But I failed and I still love you. Maybe wow leveling she needs you more compared wow leveling with me. So I tell you that world of warcraft power leveling you should love world of warcraft power leveling her and take good world of warcraft leveling care of her. You said I was so kind.world of warcraft leveling Yes, because I love you,world of warcraft leveling I hope you will be power leveling happy forever.
In English class, one girl Cheap WoW Gold said never buy WoW Gold give up when discussing. It just reminded me wow goldsomething about myself.WoW Gold Maybe it is wow power leveling something about love.WoW Gold My love, which began on July 3rd of WoW Gold 2005 and finished on August wow leveling 23rd of 2008, taught me many things. wow leveling It is not a pleasant world of warcraft power leveling thing to look back on world of warcraft power leveling that. But I know I world of warcraft power leveling must learn something from it,world of warcraft leveling no matter what it is, world of warcraft leveling happiness or sorrow.Our wow gold love did not go on so smoothly and we went through many things.
China Highlights
China Highlights
China Tours
China Hotels
China Attractions
Beijing China Travel
Shanghai China Travel
Xi'an China Travel
Guilin China Travel
Yangshuo China Travel
Post a Comment
<< Home