15.2. What are C# events?

Having reviewed the generic event model, I shall introduce C# events, and map C#'s implementation to the generic model.

A delegate instance encapsulates one or more (static or non-static) methods with the same return type and parameters, so that when the delegate instance is invoked all the methods in its invocation list are invoked in order with the same set of parameter values.

A C# event is a special type of delegate. It too encapsulates one or more methods – in this case, event handlers – so that when the event occurs, all these event handlers are invoked.

Event handlers usually take in two parameters, though this is not mandatory:

  • one object type which references the event source; and

  • one event object of type System.EventArgs ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.