I have seen various examples of event handling. Here is one: Event Sample.
Sometimes I see the delegate declared outside the class that will raise the event (as in the link above), and sometimes inside (where I think it should be declared).
It makes more sense to me to declare the event inside the class that will raise the event. The reason being that the event that the class will declare is really just some sugar coating for helper methods etc. that are really doing the adding to, subtracting from, and invoking of the delegate etc.
Are there any best practices? Are there times when you would want to declare the delegate outside, and other times where you would want to declare the delegate inside? If so, how should it be decided which to do?
Typically, these days you’d create your own class derived from
EventArgs, and then just useEventHandler<TEventArgs>– there’s no need to create a separate delegate type. So instead ofAlarmEventHandler, you’d useEventHandler<AlarmEventArgs>. TheEventArgs-derived class should generally be top-level (i.e. non-nested).