Fork me on GitHub
TypeName CopyEventToVariableBeforeFireAnalyzer
Check Id CC0016
Category Design
Severity Warning

Cause

Events should always be checked for null before being invoked.
As in a multi-threading context it is possible for an event to be unsuscribed between the moment where it is checked to be non-null and the moment it is raised, the event must be copied to a temporary variable before the check.

Example

public event System.EventHandler MyEvent;

public void Execute()
{
    MyEvent(this, System.EventArgs.Empty);
}

Code fix

A code fix will be presented to you that will transform the code:

public event System.EventHandler MyEvent;

public void Execute()
{
    var handler = MyEvent;
    if (handler != null)
        handler(this, System.EventArgs.Empty);
}

Code fix

Related rules

  • CC0031 - C#6 has a more clean way of fixing this

See also

TBD