事件EventHandler vs EventHandler [英] event EventHandler vs EventHandler
问题描述
在C#中,使用之间有根本区别
In C# Is there a fundamental difference between using
event EventHandler< myeventargs>
和 EventHandler< myeventargs>
除了使用event关键字,它们在我所看到的效果上都具有相同的效果,在智能感知中为您提供了不同的图标.
event EventHandler<myeventargs>
and
EventHandler<myeventargs>
As they both produce the same effect from what I can see apart from using the event keyword gives you a different icon in intellisense.
推荐答案
它们似乎是相似的,但确实有所不同.
They seems to be alike, but really different.
使用 event
关键字,您可以使它们像属性一样,这意味着您可以在公共场所注册它们,同时维护私有后端.
With event
keyword, you are making them something like properties, which means you can register them in public, while maintain a private back-end.
但是,没有 event
关键字,它只是一个公共委托字段,任何人都可以删除或修改其他人的事件,即"",如@Jonskeet所说.
However, without event
keyword, it's just a public delegate field, and anyone can remove or modify others' events, which is a "encapsulation disaster" as @Jonskeet said.
请检查Jon Skeet撰写的文章,它非常有用:)
Check this article by Jon Skeet, it's very helpful :)
修改:
我在上面总结的不是我最初的想法,全部归功于@Jonskeet的
What I summarized above was not my original thinking, all credits to @Jonskeet's post.
这篇关于事件EventHandler vs EventHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!