online19
online19

Reputation: 125

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?

Upvotes: 6

Views: 2274

Answers (4)

Muad'Dib
Muad'Dib

Reputation: 29226

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 ) );
}

Upvotes: 6

Deepesh
Deepesh

Reputation: 5604

INotifyPropertyChange is an event, it fires when there is any changes(changes here means value changes)in property which are binds to controls, ideally it depends on the code written in these event, otherwise it is not a overhead but it is not a good practice to have INotifyPropertyChange for every property of your class

Upvotes: 0

CodeNaked
CodeNaked

Reputation: 41393

When firing the PropertyChanged event, you should have something that looks like:

private void NotifyPropertyChanged(string name) {
    if (PropertyChanged != null) {
        PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

If PropertyChanged is null, then there are no listeners attached to your event. So there is minimal, if any, impact on performance.

If there are listeners, then you need the raise the event so they will be notified of changes.

Upvotes: 2

Tim
Tim

Reputation: 15237

If nothing is attached to the events for those objects, then there shouldn't be much of a performance penalty, though you're still executing code and so there'll be some difference compared to removing it.

Upvotes: 1

Related Questions