Events

From eqqon

(Difference between revisions)
Jump to: navigation, search
(New page: Category:CSharp = Quick Facts about C# Events = * Fireing an Event that has not been registered (==null) raises a NullReferenceException. (annoying) * Registering for the same event t...)
m (Quick Facts about C# Events)
Line 1: Line 1:
[[Category:CSharp]]
[[Category:CSharp]]
-
= Quick Facts about C# Events =
+
= 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)
* 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.
* Registering for the same event twice with the same delegate means that the delegate will be called twice.

Revision as of 13:09, 21 December 2007

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)