Events

From eqqon

Revision as of 13:09, 21 December 2007 by Henon (Talk | contribs)
Jump to: navigation, search

Events in C#

An event is just an accessor (+=, -=) to a field holding a MulticastDelegate. The difference between a publicly accessible delegate field and an event are the following:

  • you cannot

Declaration

public event <delegate_type> <event_name>;

The compiler creates a private field with the same name.

private <delegate_type> <event_name>;

Quick Facts

  • Fireing an Event that has not been registered (==null) raises a NullReferenceException. (annoying)
  • Registering for the same event twice with the same delegate means that the delegate will be called twice.
  • A delegate that has been registered for the same event multiple times must also be unregistered multiple times.
  • Unregistering an event that has never been registered (using -= on a null event) has no effect.
  • There is no built-in mechanism for preventing event recursions. (see Preventing Recursive Events)