而这一个:
public delegate void MyEventHandler(object sender,MyEventArgs e); public event MyEventHandler SomeEvent;他们都做同样的事情,我无法说出任何区别。 虽然我注意到.NET Framework的大多数类为他们的事件使用了一个自定义事件处理程序委托。这是否有特定的原因?
解决方案你是对的;他们做同样的事情。因此,您应该更喜欢前者,因为它更清晰,需要更少的输入。
许多.NET Framework类都有自己的自定义事件处理程序代表是因为在2.0版本中引入了泛型(允许缩写语法)之前写的。例如,几乎所有的WinForms库都是在泛型之前编写的,而在那些日子里,后一种形式是只有的方式。
Recently I've been wondering if there is any significant difference between this code:
public event EventHandler<MyEventArgs> SomeEvent;And this one:
public delegate void MyEventHandler(object sender, MyEventArgs e); public event MyEventHandler SomeEvent;They both do the same thing and I haven't been able to tell any difference. Although I've noticed that most classes of the .NET Framework use a custom event handler delegate for their events. Is there a specific reason for this?
解决方案You're right; they do the same thing. Thus, you should probably prefer the former over the latter because it's clearer and requires less typing.
The reason that lots of the .NET Framework classes have their own custom event handler delegates is because they were written before generics (which allowed the shorthand syntax) were introduced in version 2.0. For example, almost all of the WinForms libraries were written before generics, and back in those days, the latter form was the only way of doing things.
更多推荐
自定义EventHandler与EventHandler< EventArgs>
发布评论