更新时间:2022-01-11 01:50:30
I've written this up in a fair amount of detail in an article, but here's the summary, assuming you're reasonably happy with delegates themselves:
有关字段的事件,有一些同步,但否则添加/删除只需调用委托。的联合 / 删除更改值自动生成的场。这两个操作都分配给支持字段 - 记住,代表是不可改变的。换句话说,自动生成的code是非常喜欢这样的:
For field-like events, there's some synchronization but otherwise the add/remove just call Delegate.Combine/Remove to change the value of the auto-generated field. Both of these operations assign to the backing field - remember that delegates are immutable. In other words, the autogenerated code is very much like this:
// Backing field
// The underscores just make it simpler to see what's going on here.
// In the rest of your source code for this class, if you refer to
// ElementAddedEvent, you're really referring to this field.
private EventHandler<EventArgs> __ElementAddedEvent;
// Actual event
public EventHandler<EventArgs> ElementAddedEvent
{
add
{
lock(this)
{
// Equivalent to __ElementAddedEvent += value;
__ElementAddedEvent = Delegate.Combine(__ElementAddedEvent, value);
}
}
remove
{
lock(this)
{
// Equivalent to __ElementAddedEvent -= value;
__ElementAddedEvent = Delegate.Remove(__ElementAddedEvent, value);
}
}
}
产生磁场中的情况下,初始值为空
- 它总是会成为空
再次,如果所有用户都去掉了,因为这是Delegate.Remove的行为。
The initial value of the generated field in your case is null
- and it will always become null
again if all subscribers are removed, as that is the behaviour of Delegate.Remove.
如果你想要一个无操作处理程序来订阅你的事件,从而避免无效检查,你可以这样做:
If you want a "no-op" handler to subscribe to your event, so as to avoid the nullity check, you can do:
public EventHandler<EventArgs> ElementAddedEvent = delegate {};
在委托{}
只是不关心它的参数,并进行匿名方法罢了。
The delegate {}
is just an anonymous method which doesn't care about its parameters and does nothing.
如果有什么,目前还不清楚,请你和我会尽力帮助!
If there's anything that's still unclear, please ask and I'll try to help!