C# 分配 null 是否会从对象中删除所有事件处理程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8892579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:59:32  来源:igfitidea点击:

Does assigning null remove all event handlers from an object?

c#.netcomdelegatesevent-handling

提问by Dor Cohen

I have defined new member in my class

我在班级中定义了新成员

protected COMObject.Call call_ = null;

This class has the following event handler that I subscribed to

此类具有我订阅的以下事件处理程序

call_.Destructed += new COMObject.DestructedEventHandler(CallDestructedEvent);

Will setting my member to null as following remove the event handler?

将我的成员设置为 null 如下删除事件处理程序吗?

call_ = null;

or I have to unsubscribed with -=?

或者我必须取消订阅 -=?

采纳答案by Azodious

yes, you should use overloaded -=to unsubscribe an event.

是的,您应该使用重载-=来取消订阅事件。

simply assigning a reference to nullwill not do that automatically. The object will still be listening to that event.

简单地分配一个引用null不会自动完成。该对象仍将侦听该事件。

回答by Sai Kalyan Kumar Akshinthala

You must use the subtraction assignment operator (-=) to unsubscribefrom an event. Only after all subscribers have unsubscribed from an event, the event instance in the publisher class is set to null.

您必须使用减法赋值运算符 (-=)取消订阅事件。只有在所有订阅者都取消订阅事件后,发布者类中的事件实例才会设置为 null。

回答by VS1

You should always unsubscribe your event handlers by -= before setting to null or disposing your objects (simply setting variable to null will not unsubscribe all of the handlers), as given in the MSDN excerpt below:

在设置为 null 或处理对象之前,您应该始终通过 -= 取消订阅事件处理程序(简单地将变量设置为 null 不会取消订阅所有处理程序),如下面的 MSDN 摘录所示:

To prevent your event handler from being invoked when the event is raised, simply unsubscribe from the event. In order to prevent resource leaks, it is important to unsubscribe from events before you dispose of a subscriber object. Until you unsubscribe from an event, the multicast delegate that underlies the event in the publishing object has a reference to the delegate that encapsulates the subscriber's event handler. As long as the publishing object holds that reference, your subscriber object will not be garbage collected.

要防止在引发事件时调用您的事件处理程序,只需取消订阅该事件即可。为了防止资源泄漏,在处理订阅者对象之前取消订阅事件非常重要。在您取消订阅事件之前,发布对象中作为事件基础的多播委托具有对封装订阅者事件处理程序的委托的引用。只要发布对象持有该引用,您的订阅者对象就不会被垃圾回收。

explained at the below link in the Unsubscribingsection:

在该Unsubscribing部分的以下链接中进行了解释:

How to: Subscribe to and Unsubscribe from Events (C# Programming Guide)

如何:订阅和取消订阅事件(C# 编程指南)

More information at:

更多信息请访问:

Why you should always unscubscribe event handlers

为什么你应该总是取消事件处理程序

回答by Googol

Remove all events, assume the event is an "Action" type:

删除所有事件,假设事件是“Action”类型:

Delegate[] dary = TermCheckScore.GetInvocationList();

if ( dary != null )
{
    foreach ( Delegate del in dary )
    {
        TermCheckScore -= ( Action ) del;
    }
}