If I have a model which is pretty much a readonly collection and dispayed on a grid, where the user selects a row.
Do I need to have INotifyPropertyChanged always implemented on the model? Is there a performance benefit of implementing vs not?
I would like to know if performance is impacted by UI trying to use something like
var x = Model as INotifyPropertyChanged;
which it wouldn't have used otherwise.
If you are using the model in any data bindings, then yes, you should implement
INotifyPropertyChanged
even if it is completely immutable. The reason has nothing to do with performance, but to avoid a memory leak.A
Binding
will always try to register for change notifications, and if you do not implementINotifyPropertyChanged
or expose discrete change events of the form[Property]Changed
, it will register through thePropertyDescriptor
API. The default, reflection-basedPropertyDescriptor
implementation uses a global subscription table, and if for some reason theBinding
does not unsubscribe cleanly, your view model will be kept alive indefinitely. This is in contrast to the weak event pattern used when bindings subscribe toINotifyPropertyChanged
notifications. Bindings at runtime generally clean up after themselves, but the Xaml designer is notorious for leaking under these circumstances, causing your designer process's memory consumption to rise steadily as you work.