事件Action和事件EventHandler <eventargs>之间的区别

时间:2015-07-07 16:07:17

标签: c# coding-style eventhandler eventargs

使用Action声明事件

时出现什么问题
public interface ISomething
{
    event Action MyEventName;
}

public interface ISomething
{
    event Action<bool> MyBoolEventName;
}

而不是前一代码的其他变体使用EventHandler和EventArgs声明事件

public interface ISomething
{
     event EventHandler<EventArgs> MyEventName;
}

public class EventArgsWithBool : EventArgs
{
    private readonly bool someValue;

    public EventArgsWithBool (bool someValue)
    {
        this.someValue = someValue;
    }

    public bool SomeValue
    {
        get { return this.someValue; }
    }
}

public interface ISomething
{
     event EventHandler<EventArgsWithBool> MyBoolEventName;
}

我的想法:

这两个版本对我来说都很好,我认为第一个版本更具可读性/看起来更直接。但是一些开发人员说最好在EventArgs中使用第二种语法而不能给出很好的技术原因(除了他们知道第二种语法)。

使用第一个时是否有任何技术问题?

1 个答案:

答案 0 :(得分:1)

使用Action时,不会将Sender对象传递给事件处理程序。有时,事件处理程序知道触发事件的对象是有用的。