检查事件处理程序是否为null时,是否基于每个线程完成?
确保有人正在听这个事件就像这样:
EventSeven += new DivBySevenHandler(dbsl.ShowOnScreen);
Run Code Online (Sandbox Code Playgroud)
如果我在上面检查null的模式之后添加代码,那么为什么我需要一个空检查(从这个站点获取的代码).我错过了什么?
此外,事件和GC的规则是什么?
C#允许我们创建自定义事件访问器.
Action _custom;
public event Action Custom
{
add { _custom = (Action)Delegate.Combine( _custom, value ); }
remove { _custom = (Action)Delegate.Remove( _custom, value ); }
}
Run Code Online (Sandbox Code Playgroud)
如果未指定它们,编译器将为您创建它们.C#语言规范:
编译类似字段的事件时,编译器会自动创建存储以保存委托,并为事件创建访问器,以便向委托字段添加或删除事件处理程序.
使用dotPeek进行简化的反编译源代码public event Action Public;如下所示:
private Action Public;
public event Action Public
{
add
{
Action action = this.Public;
Action comparand;
do
{
comparand = action;
action = Interlocked.CompareExchange<Action>(
ref this.Public, comparand + value, comparand);
}
while (action != comparand);
}
remove
{
Action …Run Code Online (Sandbox Code Playgroud)