Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does unnecessary NotifyPropertyChanged calls cause performance issues?

In my new WPF Application, I am reusing a Model class. In that model class, all the properties, in their setters, fire NotifyPropertyChanged. In my application, I do not really have a use case of firing INPC for individual property. I need to know that if I keep the current architecture where individual properties fire INPC every time they get changed, will it cause any performance implications? Does it make sense to have individual properties fire INPC when it is not required?

like image 864
online19 Avatar asked May 17 '11 15:05

online19


1 Answers

Generally speaking, anytime you are running a piece of code that you don't have to, you are potentially causing performance issues.

As a rule of thumb, when you write your setters for your properties instead of just setting your backing field and raising the change event, you should check the equality before you notify, thus avoiding unnecessary updates.

for example:

public int MyInteger 
{
   get { return this._myInteger; }
   set { 
         if ( value != this._myInteger )
         {
            this._myInteger = value;
            RaiseChangedEvent("MyInteger");
         }
      }

you should also check for attached events in your RaiseChangedEvent methods, so if there are no listeners, you don't throw a null reference exception and you don't unnecessarily make a call:

private void RaiseChangedEvent(string propertyName)
{
   var changedHandler = this.PropertyChanged ;
   if ( changedHandler != null )
      changedHandler(this, new PropertyChangedEventArgs( propertyName ) );
}
like image 85
Muad'Dib Avatar answered Sep 26 '22 02:09

Muad'Dib