Monday, February 21, 2011

Why use EventArgs.Empty instead of null?

I recall reading, on multiple occasions and in multiple locations, that when firing the typical event:

protected virtual OnSomethingHappened()
{
    this.SomethingHappened(this, EventArgs.Empty);
}

e should be EventArgs.Empty if there are no interesting event args, not null.

I've followed the guidance in my code, but I realized that I'm not clear on why that's the preferred technique.

  1. Why does the stated contract prefer EventArgs.Empty over null?
  2. What sort of situations in my own code would justify a similar design decision? When should I consider creating some static "Nothing interesting here" property instead of using null to indicate the absence of something interesting?
  3. Has the addition of nullable value types impacted these decisions?
From stackoverflow
  • I believe the reasoning behind the NOT NULL is that when passed as a parameter, it is not expected for the method to need to potentially handle a null reference exception.

    If you pass null, and the method tries to do something with e it will get a null reference exception, with EventArgs.Empty it will not.

    Greg D : In what situations would you use a similar technique in your own code?
    Mitchel Sellers : If one method might pass event information and another might not. I can still call e.ToString() regardless, but one might pass a custom type and the other passes EventArgs.Empty
  • If you're using a general-purpose method which has the EventHandler signature that's called from any event handler and is passed both the object sender and EventArgs e, it can call e.ToString(), e.g., for logging events, without worrying about a null pointer exception.

  • I believe EventArgs.Empty is used to maintain the convention of passing an argument with an event, even if none are needed.

    Mitchel Sellers posted the other half of my reason halfway through my post: it prevents a null reference exception should a method try and do something with that argument (besides check if it is null).

    EventArgs.Empty basically does the work of a globally defined Event Argument with no additional information.

    EDIT

    To give a similar example of maintaining a convention- our team uses string.empty to initialize a string b/c otherwise different coders might use newString = ""; or newString = " "; or newString = null;

    All of which may produce different results for different check conditions.

    EDIT #2

    A (slightly pedantic) reason to use EventArgs.Empty Vs new EventArgs() is that the former does not initialize a new EventArgs, saving a slight amount of memory.

  • I used long time "new EventArgs()" instead of "EventArgs.Empty"... I think the important is to pass something that will not cause an Null exception.

    ICR : EventArgs.Empty isn't null, it's a static variable initialized with "new EventArgs()". It means you don't end up wasting resources initializing what is effectively a placeholder object with no varying value.
  • EventArgs.Empty is an instance of Null object pattern

    Basically as others said, having an object representing "no value" to avoid checking for null when using it.

0 comments:

Post a Comment