In a C# event handler, why must the “sender” parameter be an object?

Well, it’s a pattern rather than a rule. It does mean that one component can forward on an event from another, keeping the original sender even if it’s not the normal type raising the event.

I agree it’s a bit strange – but it’s probably worth sticking to the convention just for familiarity’s sake. (Familiarity for other developers, that is.) I’ve never been particularly keen on EventArgs myself (given that on its own it conveys no information) but that’s another topic. (At least we’ve got EventHandler<TEventArgs> now – although it would help if there were also an EventArgs<TContent> for the common situation where you just need a single value to be propagated.)

EDIT: It does make the delegate more general purpose, of course – a single delegate type can be reused across multiple events. I’m not sure I buy that as a particularly good reason – particularly in the light of generics – but I guess it’s something

Leave a Comment