Why use EventArgs.Empty instead of null?
Asked Answered
H

6

74

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. Why does the stated contract prefer EventArgs.Empty over null?

Haupt answered 9/10, 2008 at 19:1 Comment(0)
G
39

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.

Grayson answered 9/10, 2008 at 19:7 Comment(4)
In what situations would you use a similar technique in your own code?Haupt
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.EmptyGrayson
Greg, the whole point here is that the event handler is not your code. You want safety first here because you are defining and raising the event, and somebody else will write the event handler.Piercy
There are conventions and norms, if not guarantees, around events. One is the EventArgs should never be null and the handler should not have to check for it.Piercy
A
29

EventArgs.Empty is an instance of the Null object pattern.

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

Acuate answered 3/6, 2009 at 22:45 Comment(0)
A
8

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.

To give a similar example of maintaining a convention, our team uses string.Empty to initialize a string because otherwise different coders might use newString = ""; or newString = " "; or newString = null;, all of which may produce different results for different check conditions.

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.

Arapaho answered 9/10, 2008 at 19:12 Comment(2)
The "slight amount of memory" might be significant for an event that is raised many times, like a MouseMove event.Piercy
Doesn't EventArgs.Empty return an reference to an new instance of EventArgs? So where does the memory saving come from?Fukuoka
R
2

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.

Rudiger answered 9/10, 2008 at 19:10 Comment(0)
P
0

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.

Percuss answered 9/10, 2008 at 19:20 Comment(3)
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.Tsan
If there is no meaningful data in the EventArgs, don't instantiate one. EventArgs.Empty will do the job without the overhead of creating a useless object which will only be garbage collected.Piercy
I agree. Using new EventArgs() makes the intent less clear to the next programmer who comes along, and it's slightly wasteful.Avatar
R
0

from Albahari book: "in order to avoid unnecessarily instantiating an instance of EventArgs."

Reckoner answered 17/3, 2014 at 13:20 Comment(1)
null doesn't instantiate an instance of EventArgs.Haupt

© 2022 - 2024 — McMap. All rights reserved.