Take another look at Safely firing an event.  According to Grant, there’s another issue here.  The JITter may optimize away locals in situations where you think want them for thread safety.  So the recommendation to make a local copy is not as valuable as we had hoped.

 

Grant’s recommendation is to mark the event volatile:

 

    public volatile event D SomeEvent;

 

    void FireSomeEvent()

    {

        D temp = SomeEvent;

        if (temp != null)

        {

            temp();

        }

    }

 

This problem just keeps getting worse & worse!

 

My conclusion: Use the empty delegate pattern, that I described in Safely firing an event, Part 2:

 

    public event D SomeEvent = delegate { };

 

    void FireSomeEvent()

    {

        SomeEvent();

    }

 

It’s simpler, cleaner, less headache.

 

If I was going to design the language over from scratch, I would change the semantics of delegate invocation to say that if it’s empty it’s just a nop, instead of a crash.