使用事件和代表 [英] Using Events and Delegates

查看:91
本文介绍了使用事件和代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是Mani,正在学习MCA.我是新来的互联网.我在csharp中阅读了很多有关代表和事件的文章,我理解这个概念.我知道怎么用.但是我不知道我在哪里可以在asp.net中使用委托和事件.在那个话题上我完全感到困惑.任何人都可以使用asp.net和带有代码的c Sharp发送有关此主题的一些示例.

问候,
G.Manikanta

Hi,
I am Mani, Studying MCA. I new to the dotnet. I read so many articles on delegates and events in csharp i understand the concept. I know how to use it. But i dont know where can i use delegates and events in asp.net. i totally confused in that topic. Any one send some samples on this topics using asp.net and c sharp with code.

Regards,
G.Manikanta

推荐答案

只要可以使用事件,就可以使用委托.我不考虑由于您使用的库而已经绑定事件的情况.

那么,为什么要完全使用事件呢?它们本质上做了委托实例可以做的事情,但是引入了许多限制.它使编程更安全.特别是,只能在声明该事件的类中触发该事件.因此,事件用于面向事件的UI,例如代表Windows消息.

代表比事件更基本的概念.每当使用事件(向其添加处理程序)时,都会涉及一个委托实例,但是可以在没有事件的情况下使用委托.

警告!一种典型的误解是事件是多播的,而委托不是.这不是真的.一旦将一个以上的事件处理程序添加到其调用列表中,则代表实例将变为多播.另一个误解是事件需要限制使用System.EventArguments和带有System.EvenHandler的派生类(泛型或非泛型).这只是Microsoft的建议,尽管如此,我还是建议在所有情况下都遵循该建议. FxCop检测到未遵循此建议的情况,但编译器未检测到.代表没有任何特别建议.

有关更多信息,请参见 http://msdn.microsoft.com/en-us/library/17sde2xt.aspx [^ ].

—SA
You can use delegates whenever you can use events. I''m not considering the cases where you''re already bound to use events because of the library you use.

So, why using events at all? They essentially do what an delegate instance can do but introduce a number of limitations. It makes programming safer. In particular, the event can only be fired in the class where it is declared. For that reason events are used for event-oriented UI and represent Windows messages, for example.

Delegate is more basic concept than the event. Whenever you work with event (add a handler to it), a delegate instance is involved, but the delegates can be used without events.

Warning! One typical misconception is that events are multicast while delegates are not. This is not true. A delegates instance becomes multicast as soon as more than one event handler is added to its invocation list. Another misconception is that events require limitation of using System.EventArguments and derived classes with System.EvenHandler (generic or not). This is only a Microsoft recommendation, which I recommend to follow in all cases though. A failure to follow this recommendation is detected by FxCop but not compiler. There are no any special recommendations for delegates.

For more information, see http://msdn.microsoft.com/en-us/library/17sde2xt.aspx[^].

—SA


这篇关于使用事件和代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆