
Introduction
This article is meant to be a step by step guide to give its reader an understanding of Event Delegates. It's a bit daunting for newbies to .NET. But I'll try my best to break it down to beginners here...
Event Delegates as simple as 1, 2, 3
In short, event delegates aren't hard to understand... maybe the thing that perhaps makes it hard to understand is that it's not very straightforward.
Basically, we have to understand 3 things - Yes! Only 3:
- Firstly, we have a class that publishes or advertises events (let's call it
EventPublisher
). It will have a delegate and event declaration like so:
public event EventDelegate myEvent;
public delegate void EventDelegate(object from, EventArgs args);
- Our
EventSubscriber
(you can have many of this), that wants to respond to must implement a function to handle that event. It will add a reference (or connect, in simple English) to one of its methods to the EventPublisher
's class event delegate.
theClass3.myEvent += new EventPublisher.EventDelegate(this_OnProgress);
The subscriber will have a method to handle the event:
private void this_OnProgress( object sender, EventArgs e)
{
Console.WriteLine("Event handled in this_OnProgress of EventSubscriber");
Console.WriteLine("Sender:" + sender.ToString());
}
- When the
EventPublisher
class wants to raise an event, it simply calls the delegate. Now, what happens at this point is that, all the subscribers will then be asked to handle the event.
public void issueEvent(EventArgs args)
{
if (myEvent!= null)
{
myEvent(this, args);
}
}
There's a hidden last step: after you're done, simply unsubscribe the event:
~EventSubscriber()
{
theClass3.myEvent -= new EventPublisher.EventDelegate(this_OnProgress);
}
Using the code (EventPublisher.cs)

- To start, create an empty solution.
- Add a new class library project called TryThis.
- Change the default .cs name to EventPublisher.cs.
- Copy the code below (be sure to read the comments as we go on).
This guy down here is the guy who 'advertises' the event.
using System;
public class EventPublisher
{
public event EventDelegate myEvent;
public delegate void EventDelegate(object from, EventArgs args);
public void issueEvent(EventArgs args)
{
myEvent(this, args);
}
public EventPublisher()
{
}
public void SendTheEvent()
{
Console.WriteLine("We fire the event here: SendTheEvent()");
this.issueEvent(new EventArgs());
}
}
Using the code (EventSubscriber.cs)
- Add another class.
- Change the default .cs name to EventSubsriber.cs.
- Copy the code below (be sure to read the comments as we go on).
using System;
public class EventSubscriber
{
public EventSubscriber()
{
this.Start();
}
static void Main()
{
EventSubscriber theClass = new EventSubscriber();
}
private void Start()
{
EventPublisher theClass3 = new EventPublisher();
theClass3.myEvent += new EventPublisher.EventDelegate(this_OnProgress);
theClass3.SendTheEvent();
}
private void this_OnProgress( object sender, EventArgs e)
{
Console.WriteLine("Event handled in this_OnProgress of EventSubscriber");
Console.WriteLine("Sender:" + sender.ToString());
}
}
Run It! (Conclusion)
- Build the solution. You will be prompted to set the startup object, just do so.
- As you're waiting, note these points:
- Looks familiar to delegated methods, huh? Of course, it's on the same concept. A method invocation can be also called an 'event', and the method that actually 'runs' is the event handler or subscriber!
- Our delegate here can be called a multicast delegate, since we can kind of "multicast" our advertisement for the event offered. This means many subscribers may implement the event advertised.
- You're done!