Is it appropriate to fire a “property changed” event when the actual value of a property does not change?
public int SomeProperty
{
get { return this.mSomeProperty; }
set
{
this.mSomeProperty = value;
OnPropertyChanged(new PropertyChangedEventArgs("SomeProperty"));
}
}
This will trigger the event even when the new value is identical to the old value. Is this bad practice?
Best practice is not to throw the event unless the value changed.
In your case the property is just an ‘int’, so it’s just a simple equality check. If your property was an object in it’s own right, there are more cases to consider
You set the same instance again – No property change
You set a different instance with different values – Throw a property change
You set a different, but ‘equal’ instance ( i.e., the two different objects have the same set of values and can be considered equivalent from your application’s point of view ) – Throw a property change.
The last one is subject to some debate… has the property really changed when all of it’s attributes are the same? If someone is using that property change to subscribe to changes in the subclass, they will need it to know to unsubscribe from the old class and subscribe to the new one. Therefore I err on the side of announcing the change.