EventArgs.Empty澄清? [英] EventArgs.Empty clarification?

查看:231
本文介绍了EventArgs.Empty澄清?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法

protected void Item_Click(object sender, EventArgs e)
{     }

我想,其他code调用此方法。 (并不真正需要的发件人电子

是这样的:

ITEM_CLICK(NULL,NULL)

但后来我想起我可以使用 EventArgs.Empty 代替。

But then I remembered I can use EventArgs.Empty instead.

鼠标悬停在它表明:

等等...什么?

EventArgs.Empty重新presents事件?它不是。它应该重新present空的参数不是一个事件。

EventArgs.Empty represents an event ? it is not. it should represent empty argument not an event.

就像的String.Empty 重present空的字符串

just like string.empty represent empty string.

  • 在我失去了一些东西呢?

推荐答案

这只是可怜的文档,混为一谈两个概念的一个词。该最新文档似乎的更好的:

It's just poor documentation, conflating two concepts in one word. The latest documentation seems a little better:

提供一个值与事件使用没有事件数据

Provides a value to use with events that do not have event data.

麻烦的是,事件有两种不同的含义:

The trouble is that "an event" has two separate meanings:

  • 您可以订阅该事件
  • 在该事件的一个实例被提出

例如,它不会是不合理的说:订阅键preSS 事件接收键盘相关的事件

For example, it wouldn't be unreasonable to say: "Subscribe to the KeyPress event to receive keyboard-related events."

(单词代表同样糟糕,用来描述委托类型和它们的实例。)

(The word "delegate" is just as bad, used to describe delegate types and instances of them.)

这都是不幸的,但放心,你有正确的想法。

It's all unfortunate, but rest assured that you have the right idea.

顺便说一句,如果你要调用的方法的的比当事件发生时,我想可能它分成两种方法:

As an aside, if you want to call the method other than when the event is raised, I'd potentially separate it into two methods:

// This only exists to handle the event, delegating to the DoSomething method
private void HandleItemClicked(object sender, EventArgs e)
{
    DoSomething();
}

// This can be called from other code, and should be named according to what it
// does.
private void DoSomething()
{
}

如果你订阅的情况下手动,你甚至都不需要额外的方法:

If you're subscribing to the event manually, you don't even need the extra method:

item.Click += delegate { DoSomething(); };

这样,您就清楚地表明你的真正的法不关心发件人或事件参数,而你不需要调用它时,提供虚拟的值。

This way you make it clear that your "real" method doesn't care about the sender or event args, and you don't need to provide "dummy" values when calling it.

这篇关于EventArgs.Empty澄清?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆