Delegate and Event signatures
I'm designing the events and delegates that will be used in a real-time stock quoting application. All my delegates and events will be operating in a single process, so I'm leaning towards the following:
// Instrument is a stock, Tick Message has the details of the real-time quote
public delegate void TickArrivedHandler(Instrument sender, TickMessage tick);
// The Instrument publishes an OnTickArrived event so subscribers can ask to be notified when a new tick (quote) arrives
public event TickArrivedHandler OnTickArrived;
The guidelines say, "Specify two parameters named sender and e. The sender parameter represents the object that raised the event. The sender parameter is always of type object, even if it is possible to use a more specific type." I'd rather have more specific types passed in to the delegate so the subscriber can interrogate the Instrument object without a downcast, or worse, a series of "is" checks followed by downcasts. Having the check for the right type done at compile seems better than having the client doing a downcast or switch on the type at runtime.
The MS guidelines also say, "The state associated with the event is encapsulated in an instance of an event class named e. Use an appropriate and specific event class for the e parameter type." Juval Lowy contradicts this in his excellent "Programming .NET Components" book. He suggests defining the delegate to take the base EventArgs class, and "the subscriber should downcast the generic EventArgs to the specific argument class" (p. 106) I don't want my subscribers downcasting, so I'd prefer to pass the data the subscriber needs.