O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

EventHandler

Synopsis

Shortly after Beta 1 of .NET was released, Microsoft .NET developers realized that prolific use of delegates could easily lead to type-bloat; since each declared delegate in C# created a new type in the system, a large number of delegates would lead to a huge number of types to load, verify, and initialize. In Beta 2, Microsoft introduced an idiom that, it’s hoped, will keep type-bloat down to reasonable levels in .NET.

Microsoft defines two types, EventHandler (a delegate type) and EventArgs, a glorified C construct. EventHandler is declared to expect two parameters: an object reference indicating the sender of the event, and an event data parameter (the EventArgs or some derived-type instance).

This delegate represents the base type for .NET event handlers. (In Beta 2 and later, all .NET Framework Class Library types with declared events use this same idiom, so as to remain consistent.) Its arguments include a sender parameter, which refers to the object that issued the event and an e parameter, which contains additional event data. Events that do not require additional information use the EventHandler delegate directly.

Events that need to send additional information derive their own custom delegate from this type. Custom event delegates look similar, except that they replace the EventArgs parameter with a custom object derived from EventArgs. This object contains additional properties or methods that are specific to the event.

public delegate void ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required