自定义EventHandler与EventHandler< EventArgs>

编程入门 行业动态 更新时间:2024-10-27 16:37:01
本文介绍了自定义EventHandler与EventHandler< EventArgs>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 最近我一直在想知道这段代码之间是否有显着差异:

public event EventHandler&MyEventArgs> SomeEvent;

而这一个:

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&lt; EventArgs&gt;

本文发布于:2023-11-07 06:57:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565823.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   EventHandler   amp   gt   EventArgs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!