This project is read-only.

NotifyPrivate Improvement

Apr 26, 2012 at 12:06 PM

Hi,

I don't remember where, but I have read that changing the method NotifyPrivate() like this:

        void NotifyPrivate(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler == null)
                return;
            handler(this, new PropertyChangedEventArgs(propertyName));
        }

can improve performance.

Do you agree?

Apr 26, 2012 at 4:32 PM

We have been taking a look and we have similar code on SF 1.0 & 2.0: 

void NotifyPrivate(string propertyName)       
{           
if (PropertyChanged != null)               
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
}

The important thing is that we do not instantiate PropertyChangedEventArgs if there's no delegate attached to PropertyChanged event (common scenario), so I don't think there's no performance improvement here. 

There's, however, a potential concurrency problem. If the attached delegate gets removed betwheen the if condition and the execution we will get a nice NullReferenceException. I think this is unlikely since both are really small operations and only hapens on the UI that it's usually single threaded, but it could happen, so we will change the code for the next version. 

Thanks for the point!