From MSDN:
Any method that matches the delegate’s
signature, which consists of the
return type and parameters, can be
assigned to the delegate.
So how is this possible:
public delegate void AlarmEventHandler(object sender, EventArgs e);
public event AlarmEventHandler Alarm;
protected virtual void OnAlarm(EventArgs e)
{
AlarmEventHandler handler = Alarm;
if (handler != null)
{
// Invokes the delegates.
handler(this, e);
}
}
delegate AlarmEventHander and event AlarmEventHandler have different signatures yet handler can be assigned to Alarm.
Perhaps I’m mis-understanding delegates somewhat, and I would be very grateful if someone could explain where I’m going wrong.
A delegate is like a class. An event is like a property. When you declare an
eventin a class, you declare the type of event it is. In this case,AlarmEventHandler, which is an inner class of the top-level class this is a part of.In the
OnAlarmmethod, you get the instance of theAlarmEventHandlerclass that has been assigned to the event, and invoke it.To clear things up, your code above is similar to this, using normal classes & references: