Synopsis: | Always check an event handler delegate for null |
Language: | C# |
Severity Level: | 1 |
Category: | Delegates and events |
Description: |
An event that has no subscribers is null. So before invoking, always make sure that the delegate list represented by the event variable is not null. Invoke using the null conditional operator, because it additionally prevents conflicting changes to the delegate list from concurrent threads. Wrong example: event EventHandler Correct example: event EventHandler Tip You can prevent the delegate list from being empty altogether. Simply assign an empty delegate like this: event EventHandler |
Literature References: |
Aviva AV1220 |