C# 为什么使用 EventArgs.Empty 而不是 null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/188692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:17:58  来源:igfitidea点击:

Why use EventArgs.Empty instead of null?

c#eventseventargs

提问by Greg D

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.

如果没有有趣的事件参数,e 应该是 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?

采纳答案by Mitchel Sellers

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 则不会。

回答by Mark Cidade

If you're using a general-purpose method which has the EventHandlersignature that's called from any event handler and is passed both the object senderand EventArgs e, it can call e.ToString(), e.g., for logging events, without worrying about a null pointer exception.

如果你使用它具有一个通用的方法,EventHandler这是一个从任何事件处理函数调用,并传递双方签字object senderEventArgs e,它可以调用e.ToString(),例如,用于记录事件,无需担心空指针异常。

回答by ForCripeSake

I believe EventArgs.Emptyis 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 在我的帖子中途发布了我的另一半原因:如果方法尝试使用该参数执行某些操作(除了检查它是否为空),它可以防止空引用异常。

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

EventArgs.Empty基本上是在没有附加信息的情况下完成全局定义的事件参数的工作。

To give a similar example of maintaining a convention, our team uses string.Emptyto 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 = ""; or newString = " "; or newString = null;,所有这些都可能针对不同的检查条件产生不同的结果。

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

使用EventArgs.Emptyvs 的一个(有点迂腐的)原因new EventArgs()是前者不初始化 new EventArgs,从而节省了少量内存。

回答by Patrick Desjardins

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 异常的东西。

回答by Martin Konicek

EventArgs.Emptyis an instance of the Null object pattern.

EventArgs.EmptyNull 对象模式的一个实例。

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

基本上,有一个表示“无值”的对象,以避免在使用它时检查 null。

回答by Bobak_KS

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

来自阿尔巴哈里的书: "in order to avoid unnecessarily instantiating an instance of EventArgs."