我见过开发人员使用下面的代码.它们之间的确切区别是什么,哪些符合标准?他们是一样的,Action而且Func<T>是一个代表,以及:
public event Action<EmployeeEventAgs> OnLeave;
public void Leave()
{
OnLeave(new EmployeeEventAgs(this.ID));
}
Run Code Online (Sandbox Code Playgroud)
VS
public delegate void GoOnLeave(EmployeeEventAgs e);
public event GoOnLeave OnLeave;
public void Leave()
{
OnLeave(new EmployeeEventAgs(this.ID));
}
Run Code Online (Sandbox Code Playgroud) 从这个问题(和其他人)中可以看到在调用之前为事件处理程序值创建快照的示例:
var tmp = _myEventHandler;
if(tmp != null) {
tmp(sender, args);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将事件处理程序和args传递给函数,这会做同样的事情吗?
protected void Invoke(MyEventHandler handler, MyEventArgs args)
{
if (handler != null)
handler(this, args);
}
Run Code Online (Sandbox Code Playgroud)
我会说是的,但在考虑之后我不知道它是否相同 - 就像优化过程内联这个函数并删除快照变量一样?