Skip to content

Gendarme.Rules.Performance.ConsiderCustomAccessorsForNonVisibleEventsRule(2.10)

Sebastien Pouliot edited this page Jan 22, 2011 · 2 revisions

ConsiderCustomAccessorsForNonVisibleEventsRule

Assembly: Gendarme.Rules.Performance
Version: 2.10

Description

This rule looks for non-visible events to see if their add/remove accessors are the default ones. The default, compiler generated, accessor is marked as synchronized which means that the runtime will bracket them between Monitor.Enter and Monitor.Exit calls. This is the safest approach unless, for non-visible events, you have a performance bottleneck around the events. In this case you should review if your code needs the locks or if you can provide an alternative to them.

Examples

Bad example:

private event EventHandler<TestEventArgs> TimeCritical;

Good example:

static object TimeCriticalEvent = new object ();
EventHandlerList events = new EventHandlerList ();
private event EventHandler<TestEventArgs> TimeCritical {
    add {
        events.AddHandler (TimeCriticalEvent, value);
    }
    remove {
        events.AddHandler (TimeCriticalEvent, value);
    }
}

Notes

  • This rule is available since Gendarme 2.0
Clone this wiki locally