相关疑难解决方法(0)

Action <T> vs委托事件

我见过开发人员使用下面的代码.它们之间的确切区别是什么,哪些符合标准?他们是一样的,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)

c# delegates

58
推荐指数
6
解决办法
5万
查看次数

将事件处理程序值快照作为C#中的方法参数

从这个问题(和其他人)中可以看到在调用之前为事件处理程序值创建快照的示例:

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)

我会说是的,但在考虑之后我不知道它是否相同 - 就像优化过程内联这个函数并删除快照变量一样?

c# events delegates

1
推荐指数
1
解决办法
98
查看次数

标签 统计

c# ×2

delegates ×2

events ×1