C Sharp Events

From Leo's Notes
Revision as of 23:27, 1 November 2014 by 10.1.1.3 (talk) (Created page with " An event in C# contains two components: # An event handler # The actual event The event handler is a delegate which defines what arguments the event will contain. You can al...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
This page was 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();
}