C# Event Example: EventHandlerUse an event type with an EventHandler delegate. Call Invoke to run all attached methods on the event.
Event. An event can have many handlers. With the event handler syntax, we create a notification system. We attach additional methods without changing other parts of the code. This makes programs easier to maintain.
this example shows the event keyword. It creates an original event. The .NET Framework has many existing events: these are covered elsewhere. The delegate keyword is used to specify the EventHandler type.
Event: The event keyword is used to create an instance of an event that can store methods in its invocation list.
In Main: The _show event has 4 method instances added to its invocation list with the plus "+=" operator.
Next: In the invocation list of the _show event, we add 4 events: Dog, Cat and 2 instances of Mouse.
Finally: When the Invoke method is called, each of those method instances are called. The strings are printed to the console.Console
C# program that uses event
public delegate void EventHandler();
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.
static void Cat()
static void Dog()
static void Mouse()
In this program, we use the static modifier on the event. The example was designed this way to enable the event to be accessed directly in the static Main method: for simplicity.
Static: In program design, avoiding static instances is worthwhile. It can lead to many dependencies and excess complexity.Static
Discussion. Events let you add 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.
Events 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. This reduces the number of changes to the code.
Tip: The event handler notification system provides a way to isolate different parts of your program from excessive changes.
Events are used in many objects in the .NET Framework. The Windows Forms framework uses events for many controls. These events allow you to instantly act upon button clicks and key presses.
Note: With threading, we use events on the BackgroundWorker type. We can monitor file system changes with FileSystemWatcher.BackgroundWorkerFileSystemWatcher
Notifications are important. A class can provide notifications with an event. And then all other parts of the program can handle these events with event handlers. This is explained in the C# specification.
Quote: An event is a member that enables an object or class to provide notifications. Clients can attach executable code for events by supplying event handlers (The C# Programming Language).
With events we can create a specialized language. A language is described with three principles. The first principle is use of primitive operations. The second is a way to combine elements, and the third is an ability to specify methods.
Quote: A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize our ideas about processes (Structure and Interpretation of Computer Programs).
the methods added to the event are the primitive functions. By adding event handler instances to an event, we achieve a means of combination. And the methods serve as a means of abstraction for a real-world model.
Caution: You can create an event with nearly any code construct. This material has little relation to the "event" C# keyword.
Summary. With events we separate something that happens from the behaviors we want to occur. And this leads to higher-quality programs. Events are often used on the built-in types in the .NET Framework.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.