Setting the event to Null

I have a code like this:

public class SomeClass { int _processProgress; public int ProcessProgress { get { return _processProgress; } set { _processProgress = value; if (ProcessProgressChanged != null) ProcessProgressChanged(value); } } public delegate void ProcessProgressChangedEventHandler(int progressPercentage); public event ProcessProgressChangedEventHandler ProcessProgressChanged; public void ClearProcessProgressChangedEvent() { this.ProcessProgressChanged = null; } } 

Will it unsubscribe from all methods in the ProcessProgressChanged event when I call the ClearProcessProgressChangedEvent() method?

My code is in C #, framework 4, in VS2010 Pro, the project is in Winforms.

Please, help. Thanks in advance.

+7
source share
2 answers

Well, it will effectively clear the list of subscribers, yes (setting the delegate base field to null ) - so that the next time ProcessProgress set, the handlers will not be called. Actually this does not mean that the event is null - it sets the base field to null . It's just that the C # compiler creates both an event (a pair of subscription / unsubscribe methods) and a field (for storing handlers) using a single declaration.

You can find a useful article.

+14
source

Yes, he will unsubscribe from all events. There is a link (indirect IMHO bit) to this one here :

When all subscribers have unsubscribed from the event, the event instance in the publisher class is set to null .

+3
source

All Articles