I'm understanding how events work in C# (am a fair newbie in this field). What I'm trying to understand is why we use events.
Do you know a well coded / architected app which uses events?
You generally use events to notify subscribers about some action or state change that occurred on the object. By using an event, you let different subscribers react differently, and by decoupling the subscriber (and its logic) from the event generator, the object becomes reusable.
Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.
Yes, you can declare an event without declaring a delegate by using Action. Action is in the System namespace.
Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts.
To provide a concrete normal world example....
You have a form, the form has a listbox. There's a nice happy class for the listbox. When the user selects something from the listbox, you want to know, and modify other things on the form.
Without events:
You derive from the listbox, overriding things to make sure that your parent is the form you expect to be on. You override a ListSelected method or something, that manipulates other things on your parent form.
With events: Your form listens for the event to indicate a user selected something, and manipulates other things on the form.
The difference being that in the without events case you've created a single-purpose class, and also one that is tightly bound to the environment it expects to be in. In the with events case, the code that manipulates your form is localized into your form, and the listbox is just, well, a listbox.
What would be very useful is a non trivial example of an app which uses events (guess it really helps testing too?)
Thoughts so far are:
Why use Events or publish / subscribe?
Any number of classes can be notified when an event is raised.
The subscribing classes do not need to know how the Metronome (see code below) works, and the Metronome does not need to know what they are going to do in response to the event
The publisher and the subscribers are decoupled by the delegate. This is highly desirable as it makes for more flexible and robust code. The metronome can change how it detects time without breaking any of the subscribing classes. The subscribing classes can change how they respond to time changes without breaking the metronome. The two classes spin independently of one another, which makes for code that is easier to maintain.
class Program
{
static void Main()
{
// setup the metronome and make sure the EventHandler delegate is ready
Metronome metronome = new Metronome();
// wires up the metronome_Tick method to the EventHandler delegate
Listener listener = new Listener(metronome);
ListenerB listenerB = new ListenerB(metronome);
metronome.Go();
}
}
public class Metronome
{
// a delegate
// so every time Tick is called, the runtime calls another method
// in this case Listener.metronome_Tick and ListenerB.metronome_Tick
public event EventHandler Tick;
// virtual so can override default behaviour in inherited classes easily
protected virtual void OnTick(EventArgs e)
{
// null guard so if there are no listeners attached it wont throw an exception
if (Tick != null)
Tick(this, e);
}
public void Go()
{
while (true)
{
Thread.Sleep(2000);
// because using EventHandler delegate, need to include the sending object and eventargs
// although we are not using them
OnTick(EventArgs.Empty);
}
}
}
public class Listener
{
public Listener(Metronome metronome)
{
metronome.Tick += new EventHandler(metronome_Tick);
}
private void metronome_Tick(object sender, EventArgs e)
{
Console.WriteLine("Heard it");
}
}
public class ListenerB
{
public ListenerB(Metronome metronome)
{
metronome.Tick += new EventHandler(metronome_Tick);
}
private void metronome_Tick(object sender, EventArgs e)
{
Console.WriteLine("ListenerB: Heard it");
}
}
Full article I'm writing on my site: http://www.programgood.net/
nb some of this text is from http://www.akadia.com/services/dotnet_delegates_and_events.html
Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With