C#我应该手动删除声明的事件处理程序吗? [英] C# Should I manually remove the event handler I declared?
本文介绍了C#我应该手动删除声明的事件处理程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,在这里举个例子:
Okay, make an example here:
- 我有UserControl A,UserControl B,UserControl C和一个Windows窗体.
- 此Windows窗体仅以UserControl A启动.
- UserControl C具有[Next]和[Back]按钮.
- 说,UserControl A是用事件处理程序声明的.实际上,UserControl A中的一个功能将引发事件调用以在UserControl C中执行一个功能.
- 因此,在UserControl C,我必须添加
"UserControlA.OneFunction + = this.UserControlC_Function;"
"UserControlA.OneFunction += this.UserControlC_Function;"
- 如果我单击UserControl C的下一步"按钮,它将处置UserControl A并将新的UserControl B添加到Windows窗体.但我从不手动删除此事件处理程序.
调用方(声明事件的位置)是UserControl A中的功能之一.
UserControl C中的功能之一是侦听器.
One of the function in UserControl A is the caller (where event is declared).
One of the function in UserControl C is the listener.
所以,这是我的问题:
- 我应该在删除UserControl A之前手动删除处理程序吗?
- 此用户控件A会自动删除先前声明的处理程序吗?
- 我应该将此添加到某处吗?
"UserControlA.OneFunction-= this.UserControlC_Function;"
"UserControlA.OneFunction -= this.UserControlC_Function;"
推荐答案
- 按照惯例,我们不这样做.而且由于处置后不应调用任何事件 ,因此除非有问题的控件出现异常行为,否则无需这样做.
- 不.至少没有从反射器看到的代码.
- By convention, we don't. And since no event should be invoked after disposal, there is no need to do so unless the control in question is behaving weirdly.
- No. At least there isn't such code as seen from reflector.
这篇关于C#我应该手动删除声明的事件处理程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文