事件Action和事件eventandler 的区别
本文关键字:事件 区别 EventArgs Action eventandler | 更新日期: 2023-09-27 18:01:21
用Action
声明事件时出现了什么问题?public interface ISomething
{
event Action MyEventName;
}
或
public interface ISomething
{
event Action<bool> MyBoolEventName;
}
而不是前面代码的其他变体用eventandler和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,而不能给出良好的技术理由(除了他们知道第二种语法)。
使用第一个时是否会遇到任何技术问题?
当您使用Action时,您没有将Sender对象传递给事件处理程序。有时候,让事件处理程序知道是什么对象触发了事件是很有用的。