1
Vote

EventHandlerHelper event raising is error-prone

description

The code for raising an event is not that recommended in Framework Design Guidelines by Cwalina and Abrams; it does not make a local copy and check for null before invoking. See http://msdn.microsoft.com/en-us/library/vstudio/edzehd2t.aspx for the following example:
class Counter
{
    public event EventHandler ThresholdReached;

    protected virtual void OnThresholdReached(EventArgs e)
    {
        EventHandler handler = ThresholdReached;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    // provide remaining implementation for the class
}

comments

pckujawa wrote Jul 31, 2013 at 6:03 PM

I may have forgotten that passing the event handler as a parameter makes a local copy, so the copy step within the method is redundant (see comments at http://stackoverflow.com/questions/12644079/how-to-create-a-generic-event-raising-method). So maybe the code is equivalent. Sorry about that.