[英]what's the purpose of static read only 'Empty' in EventArgs
I'm new to C#, just a question on EventArgs class. 我是C#的新手,只是有关EventArgs类的问题。 We know the definition is :
我们知道定义是:
public class EventArgs
{
public static readonly EventArgs Empty;
public EventArgs();
}
and I guess one of the use is when you don't care about passing custom information 我猜其中一种用途是当您不关心传递自定义信息时
public event EventHandler<EventArgs> TestHappening;
private void MyMethod()
{
TestHappening( this, EventArgs.Empty );
}
but can't we achieve the same goal by using: 但是我们不能通过使用以下方法实现相同的目标:
TestHappening( this, new EventArgs() );
It's preferable to use EventArgs.Empty
for two reasons: 最好使用
EventArgs.Empty
有两个原因:
Checking if arguments are empty is convenient 检查参数是否为空很方便
EventArgs newArgs = new EventArgs(); EventArgs emptyArgs = EventArgs.Empty; Console.WriteLine(newArgs == EventArgs.Empty); // false Console.WriteLine(emptyArgs == EventArgs.Empty); // true
EventArgs.Empty is just define an instance of EventArgs without data. EventArgs.Empty只是定义没有数据的EventArgs的实例。 In C# there is a lot of class with defined property to declare a state or a kind or a value of this class to support who are just approach C#.
在C#中,有很多具有定义属性的类来声明此类的状态或种类或值,以支持仅使用C#的人员。
string.Empty
, int.MaxValue
, etc are example for this 例如
string.Empty
, int.MaxValue
等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.