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.
如果没有有趣的事件参数,则应该是EventArgs.Empty,而不是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?
我遵循了我的代码中的指导,但我意识到我不清楚为什么这是首选技术。为什么声明的合约更喜欢EventArgs.Empty而不是null?
6 个解决方案
#1
32
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.
我相信NOT NULL背后的原因是当作为参数传递时,不希望该方法需要潜在地处理空引用异常。
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.
如果你传递null,并且该方法试图用e做某事,它将获得一个空引用异常,而EventArgs.Empty则不会。
#2
26
EventArgs.Empty
is an instance of the Null object pattern.
EventArgs.Empty是Null对象模式的一个实例。
Basically, having an object representing "no value" to avoid checking for null when using it.
基本上,使用一个表示“无值”的对象来避免在使用它时检查null。
#3
7
I believe EventArgs.Empty
is used to maintain the convention of passing an argument with an event, even if none are needed.
我相信EventArgs.Empty用于维护使用事件传递参数的约定,即使不需要也是如此。
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).
Mitchel Sellers在我的帖子中间发布了另一半我的理由:如果方法尝试并使用该参数执行某些操作,它会阻止空引用异常(除了检查它是否为null)。
EventArgs.Empty
basically does the work of a globally defined Event Argument with no additional information.
EventArgs.Empty基本上完成了全局定义的Event Argument的工作,没有其他信息。
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.
为了给出一个维护约定的类似示例,我们的团队使用string.Empty来初始化一个字符串,因为否则不同的编码器可能会使用newString =“”;或者newString =“”;或者newString = null;,所有这些都可能针对不同的检查条件产生不同的结果。
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.
使用EventArgs.Empty与新EventArgs()的一个(略显迂腐)原因是前者没有初始化新的EventArgs,节省了少量内存。
#4
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.
如果您正在使用具有从任何事件处理程序调用的EventHandler签名并且同时传递对象发送方和EventArgs e的通用方法,则它可以调用e.ToString(),例如,用于记录事件,而无需担心空指针异常。
#5
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.
我用了很长时间“new EventArgs()”而不是“EventArgs.Empty”...我认为重要的是传递一些不会导致Null异常的东西。
#6
0
from Albahari book: "in order to avoid unnecessarily instantiating an instance of EventArgs."
来自Albahari的书:“为了避免不必要地实例化EventArgs的实例。”
#1
32
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.
我相信NOT NULL背后的原因是当作为参数传递时,不希望该方法需要潜在地处理空引用异常。
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.
如果你传递null,并且该方法试图用e做某事,它将获得一个空引用异常,而EventArgs.Empty则不会。
#2
26
EventArgs.Empty
is an instance of the Null object pattern.
EventArgs.Empty是Null对象模式的一个实例。
Basically, having an object representing "no value" to avoid checking for null when using it.
基本上,使用一个表示“无值”的对象来避免在使用它时检查null。
#3
7
I believe EventArgs.Empty
is used to maintain the convention of passing an argument with an event, even if none are needed.
我相信EventArgs.Empty用于维护使用事件传递参数的约定,即使不需要也是如此。
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).
Mitchel Sellers在我的帖子中间发布了另一半我的理由:如果方法尝试并使用该参数执行某些操作,它会阻止空引用异常(除了检查它是否为null)。
EventArgs.Empty
basically does the work of a globally defined Event Argument with no additional information.
EventArgs.Empty基本上完成了全局定义的Event Argument的工作,没有其他信息。
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.
为了给出一个维护约定的类似示例,我们的团队使用string.Empty来初始化一个字符串,因为否则不同的编码器可能会使用newString =“”;或者newString =“”;或者newString = null;,所有这些都可能针对不同的检查条件产生不同的结果。
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.
使用EventArgs.Empty与新EventArgs()的一个(略显迂腐)原因是前者没有初始化新的EventArgs,节省了少量内存。
#4
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.
如果您正在使用具有从任何事件处理程序调用的EventHandler签名并且同时传递对象发送方和EventArgs e的通用方法,则它可以调用e.ToString(),例如,用于记录事件,而无需担心空指针异常。
#5
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.
我用了很长时间“new EventArgs()”而不是“EventArgs.Empty”...我认为重要的是传递一些不会导致Null异常的东西。
#6
0
from Albahari book: "in order to avoid unnecessarily instantiating an instance of EventArgs."
来自Albahari的书:“为了避免不必要地实例化EventArgs的实例。”