// 设备启动事件
public event EventHandler<StartArgs> EventStart;
public class StartArgs : EventArgs { /* 空 */ } // 设备停止事件
public event EventHandler<StopArgs> EventStop;
public class StopArgs : EventArgs { /* 空 */ }
因所需信息均可从 sender 获取,故如此,不知妥否。
public event EventHandler<StartArgs> EventStart;
public class StartArgs : EventArgs { /* 空 */ } // 设备停止事件
public event EventHandler<StopArgs> EventStop;
public class StopArgs : EventArgs { /* 空 */ }
因所需信息均可从 sender 获取,故如此,不知妥否。
顺便,EventHandler<T> 会自己检查空值吗,if (EventStart != null) EventStart(this, null); 有无必要?
没有必要EventStart(this, new StartArgs())
也没有必要声明2个参数类
更没必要if (EventStart != null)
using System.Collections.Generic;
using System.Linq;
using System.Text;class MyT
{
public event EventHandler<StartEventArgs> Started;
public class StartEventArgs : EventArgs { }
public void Start()
{
Console.WriteLine("MyT Start!");
Started(this, null); // 直接来试试
}
}namespace test5
{
class Program
{
static void Fun1(object sender, EventArgs e)
{
Console.WriteLine("Fun1");
}
static void Main(string[] args)
{
MyT myt = new MyT();
//myt.Started += Fun1;
myt.Start();
}
}
}还真的有异常,未处理的异常: System.NullReferenceException: 未将对象引用设置到对象的实例。
在 MyT.Start() 位置 D:\test5\Program.cs:行号 13
在 test5.Program.Main(String[] args) 位置 D:\test5\Program.cs:行号 29