1

What's the reasoning behind copying a event handler to a local variable before invoking it? Resharper does this when creating a invocator, and I remember looking at a code sample on CodeProject that does this.

Code:

  public class MyObject
  {
    public event EventHandler SomethingHappened;

    protected virtual void OnSomethingHappened()
    {
      var handler = SomethingHappened; // Why this?
      if (handler != null)
        handler(this, EventArgs.Empty);
    }
  }
Cameron
  • 2,574
  • 22
  • 37

0 Answers0