Setting an Event to Null

前端 未结 2 672
你的背包
你的背包 2021-02-07 13:07

I have a code like this:

public class SomeClass
{
    int _processProgress;
    public int ProcessProgress 
    { 
        get { return _processProgress; } 
             


        
相关标签:
2条回答
  • 2021-02-07 13:17

    Well, it'll effectively clear the list of subscribers, yes (by setting the underlying delegate field to null) - so that the next time ProcessProgress is set, no handlers will be called. It's not really setting the event to null - it's setting the underlying field to null. It's just that the C# compiler is creating both an event (a subscribe/unsubscribe pair of methods) and a field (to store the handlers) using a single declaration.

    You may find my article about events and delegates useful.

    Note that your event-raising code currently isn't thread-safe. I don't know whether it needs to be or not, but you might want to consider using:

    set 
    { 
        _processProgress = value; 
        var handlers = ProcessProgressChanged;
        if (handlers != null) 
        {
            handlers(value);
        }
    }
    

    That way you won't get a NullReferenceException if the last handler is unsubscribed after the nullity check but before the invocation.

    0 讨论(0)
  • 2021-02-07 13:25

    Yes, it will unsubscribe everyone from the event. There is a (bit indirect IMHO) reference to this here:

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

    0 讨论(0)
提交回复
热议问题