Do not declare event fields as virtual
Do not declare event fields as virtual
Microsoft docsDescription
Follow these .NET design guidelines to raise base class events in derived classes. Do not declare virtual events in a base class. Overridden events in a derived class have undefined behavior. The C# compiler does not handle this correctly and it is unpredictable whether a subscriber to the derived event will actually be subscribing to the base class event.
Cause
A field-like event was declared as virtual.
By default, this rule only looks at externally visible types, but this is configurable.
How to fix violations
Follow these .NET design guidelines and avoid virtual field-like events.
Example
using System;
public class C
{
// CA1070: Event 'ThresholdReached' should not be declared virtual.
public virtual event EventHandler ThresholdReached;
}When to suppress
If the event is an externally visible public API that is already part of a shipped library, then it is safe to suppress a warning from this rule to avoid a breaking change for the library consumers.