Reputation: 2445
I need to unsubscribe all delegates subscribed on my event. But I found two ways to do it..
class Cls
{
delegate void doDel();
static event doDel doE;
void Uns
{
//first
foreach (doDel item in doE.GetInvocationList())
{
doE -= item;
}
//second
doE = null;
}
}
What difference between? And what way is best and why?
Upvotes: 1
Views: 166
Reputation: 1659
First approach is quite correct. As MSDN states:
Use the subtraction assignment operator (-=) to unsubscribe from an event:
publisher.RaiseCustomEvent -= HandleCustomEvent;
When all subscribers have unsubscribed from an event, the event instance in the publisher class is set to null.
Upvotes: 1