以下代码来自 Josh Smith 的 MVVM 示例:
/// <summary>
/// Raised when this workspace should be removed from the UI.
/// </summary>
public event EventHandler RequestClose;
void OnRequestClose()
{
//if (RequestClose != null)
// RequestClose(this, EventArgs.Empty);
EventHandler handler = this.RequestClose;
if (handler != null)
handler(this, EventArgs.Empty);
}
注释行是我添加的。我的问题是注释行会与未注释行做同样的事情,对吗?那么为什么要创建另一个 EventHandler 引用呢?或者我在这里遗漏了什么?谢谢
坦莫伊是对的。这样做是为了防止在“if”之后但在“RequestClose()”之前的其他线程中 RequestClose 被更改(例如,为 null)的可能性。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)