4

-- If I define an event with an inital empty delegate I don't need to check for null

class MyClass
{
 public event EventHandler<MyEventArgs> MyEvent = delegate { };

 void SomeMethod()
 {
  ...
  MyEvent(); // No need to check for null
  ...
 }
}

-- Otherwise I need to check for null

class MyClass
{
 public event EventHandler<MyEventArgs> MyEvent;

 void SomeMethod()
 {
  ...
  if (MyEvent != null) // No need to check for null
   MyEvent(); 
  ...
 }
}

What's the difference between these? In what cases one is better than another?

Thanks

John Topley
  • 113,588
  • 46
  • 195
  • 237
NKC1
  • 43
  • 2

3 Answers3

3

The upvoted answer is dramatically wrong, I have to post an answer. Somebody is wrong on the Internet, can't come to bed just yet.

It is convenient but it doesn't come for free. The compiler has to generate a class for the anonymous method and the JIT compiler has to generate code for it. And that code always executes when you raise the event, whether or not a client has subscribed an event handler. The null check code also always executes, but that takes a lot less code and time.

This isn't a lot of code and a lot of time. The null check takes 2 machine code instructions and should execute in a single CPU cycle. The anonymous delegate takes about an order of magnitude more but that's still not a lot on a modern machine. Personally, I'm too old to be wasteful like that, two invariably is my choice.

Not in the least because that's the standard pattern, everybody recognizes it.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • The worse thing about always attaching the dummy event handler is that delegate handling methods and delegate invocation are optimized for the case of a delegate performing a single action. Adding a delegate to null is easy, as is removing a delegate from itself. Although Microsoft designed the MultiCastDelegate for fast invocation, there's still an extra layer of indirection that wouldn't be necessary with a single direct delegate. – supercat Mar 07 '11 at 18:29
2

First one is applicable solution, but it has very very little performance penalty to call extra empty delegate.

Second solution is not thread safe (if it matters for you, of cause).

You should use following:

var handler = MyEvent;
if (handler != null )
  handler(this, new MyEventArgs());
Sergey Teplyakov
  • 11,477
  • 34
  • 49
1

At our company we wrote an extension method which hooks onto most events, and checks if it's null before invoking.

We reduced this:

var handler = MyEvent;
if (handler != null)
{
    handler(...);
}

to

MyEvent.SafeTrigger(...);
Andy Shellam
  • 15,403
  • 1
  • 27
  • 41
  • @Andy: check out this thread: http://stackoverflow.com/questions/2282894/event-handler-raising-method-convention – Hans Passant Mar 05 '10 at 11:58
  • Yeah inside the SafeTrigger method that's what it does. Because SafeTrigger is an extension method, if MyEvent is null, the extension method gets a null argument. – Andy Shellam Mar 05 '10 at 12:21