
Events can have many handlers. With the event handler syntax in the C# language, we create a notification system. In this way we attach additional methods without changing other parts of the code. This makes programs more maintainable.
KeywordsThis C# example program uses an event type. Events can have multiple EventHandlers.

First, this example demonstrates the event keyword in the C# language and how you can create an original event; the .NET Framework has many existing events which are covered elsewhere. The delegate keyword is used to specify the EventHandler type. Next, the event keyword is used to create an instance of an event that can store methods in its invocation list.
Program that uses event handler [C#]
using System;
public delegate void EventHandler();
class Program
{
public static event EventHandler _show;
static void Main()
{
// Add event handlers to Show event.
_show += new EventHandler(Dog);
_show += new EventHandler(Cat);
_show += new EventHandler(Mouse);
_show += new EventHandler(Mouse);
// Invoke the event.
_show.Invoke();
}
static void Cat()
{
Console.WriteLine("Cat");
}
static void Dog()
{
Console.WriteLine("Dog");
}
static void Mouse()
{
Console.WriteLine("Mouse");
}
}
Output
Dog
Cat
Mouse
Mouse
Main method description. The program's control flow begins in the Main entry point. The _show event has four method instances added to its invocation list with the += operator. This has nothing to do with the arithmetic addition operator except for the syntax.
Add events. In the invocation list of the _show event, we add four events: the Dog, Cat, and two instances of the Mouse event. Finally, when the Invoke method is called, each of those method instances are called and the strings are printed to the console.

What's the big deal with events in the C# language? The key point here is that events let you add additional methods to be triggered upon an external event. In your program design, you might add new event handlers dynamically for a specific action. Then, at some point the Invoke method could be called.
Reduce excessive changes. At the part of the code where you call Invoke to signify the event, you would not need to know all the different handlers that were added. The event handler notification system, then, provides a way to isolate different parts of your program from excessive changes. This can reduce coding errors and result in higher-quality software.

Events are used in many different objects in the .NET Framework. The Windows Forms framework uses events for many different controls and allows you to instantly act upon button clicks and key presses. With threading, you can use events on the BackgroundWorker type. You can also use events to monitor what changes occur on the file system with FileSystemWatcher.
Windows Forms BackgroundWorker FileSystemWatcher
Events can be used to create a specialized language. Languages can be described with three important principles: the primitives in the language; the capacity to combine different elements; and the ability to specify methods.
With events, the methods you add to the event can be considered the primitive functions. By adding event handler instances to an event, you can achieve a means of combination. And finally, the methods can also serve as a means of abstraction for a real-world model.
Structure and Interpretation of Computer Programs
Typically, events in the C# language are used on the built-in types developed for the .NET Framework itself. However, for certain programs that need to attach many behaviors to different actions, custom events can provide an additional level of indirection. You can completely separate something that happens from the behaviors you want to occur.
Delegate Tutorial