C Sharp Events

From Leo's Notes
Last edited on 1 November 2014, at 23:27.

An event in C# contains two components:

  1. An event handler
  2. The actual event

The event handler is a delegate which defines what arguments the event will contain. You can always use the EventHandler base class with the 'sender' and 'EventArgs' arguments.

The actual event will use the event handler with a given name.

private delegate void StationChangedEventHandler();
private event StationChangedEventHandler StationChanged;

protected virtual void OnStationChanged() {
	StationChangedEventHandler handler = this.OnStationChanged;
	if (handler != null)
		handler();
}