Is there a difference between this two variants (performance, memory leaks or guidelines)?
With NPC:
private ICommand mGoBackCommand;
public ICommand GoBackCommand
{
get { return mGoBackCommand; }
set
{
if (mGoBackCommand != value)
{
mGoBackCommand = value;
RaisePropertyChanged("GoBackCommand");
}
}
}
Auto property:
public ICommand GoBackCommand {get; set;}
UPD:
Final question is:
Can I use auto properties in VievModel if they are simple commands which will be assigned once in constructor, or I need implement NPC on each property of the VM because of performance, memory leaks or something else?
If you are writing a class that implements
INotifyPropertyChanged, you are making a contract that says “I’ll raise thePropertyChangedevent when any property changes.”But, if you know for a fact that a property won’t change during the lifetime of the instance, then you trivially satisfy that contract by never raising
PropertyChangedfor that property.So, if you set a property once in the constructor (a “set it a forget it” property), then you don’t need to mangle the property just to support
INotifyPropertyChangedand you can use an auto-implemented property. However, in this you should change the property from this:to this:
so that it cannot be accidentally modified outside the class.