HomeSearch

C# event Examples

Use 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. Events are used in many .NET programs (including Windows Forms).
Event details. We attach additional methods without changing other parts of the code. This makes programs easier to maintain. It makes code clearer.KeywordsDelegates
First example. We use the event keyword. It creates an original event. The .NET Framework has many existing events: these are covered elsewhere.

Info: The delegate keyword is used to specify the EventHandler type. The event keyword is used to create an instance of an event.

Static: We use the static modifier on the event. We can access the static event from a static method (Main).

Static

Part 1: The _show event has 4 method instances added to its invocation list with the plus "+=" operator.

Part 2: The Invoke method is called. This in turn causes each of the 4 method instances to be run. The strings are printed to the console.

Console
C# program that uses event using System; public delegate void EventHandler(); class Program { public static event EventHandler _show; static void Main() { // Part 1: add event handlers to Show event. _show += new EventHandler(Dog); _show += new EventHandler(Cat); _show += new EventHandler(Mouse); _show += new EventHandler(Mouse); // Part 2: 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
GetInvocationList. We can call methods on an event type. Here we use GetInvocationList, which returns a Delegate object array. With reflection, we can print the names of the methods.ForeachObject ArrayReflection
C# program that uses GetInvocationList using System; public delegate void EventHandler(); class Program { public static event EventHandler _click; static void Main() { _click += new EventHandler(ClickItem); _click += new EventHandler(ClickItem); // Display all delegates. foreach (var item in _click.GetInvocationList()) { Console.WriteLine(item.Method.Name); } } static void ClickItem() { } } Output ClickItem ClickItem
A discussion. Events let you add methods to be triggered upon an external event. You might add new event handlers dynamically for a specific action. Then, call Invoke to run them all.

Tip: Events can reduce the number of code changes needed. We can just add events, and not worry about calling locations.

Thus: The event handler notification system provides a way to isolate different parts of your program from excessive changes.

Uses, Windows Forms. Events are used in many objects in the .NET Framework. The Windows Forms framework uses events for many controls.

Tip: 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
Research. 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).

A summary. With events we separate something that happens from the behaviors we want to occur. Events are often used on the built-in types in the .NET Framework.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.