Fork me on GitHub
TypeName UseInvokeMethodToFireEventAnalyzer
Check Id CC0031
Category Design
Severity Warning

Cause

In C#6 a delegate can be invoked using the null-propagating operator (?.) and it’s invoke method to avoid throwing a NullReference exception when there is no method attached to the delegate.

Example

public class MyClass
{
    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 class MyClass
{
    public event System.EventHandler MyEvent;
    public void Execute()
    {
        MyEvent?.Invoke(this, System.EventArgs.Empty);
    }
}

Code fix

Related rules

  • CC0016 - Won’t be suggested if the handler is copied to a local variable

See also

TBD