I am getting into MVVM and stumbled upon two versions of calling the OnPropertyChanged
function shown in this MWE:
public class SampleModel : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (value == _name) return;
_name = value;
// Version 1
OnPropertyChanged();
// Version 2
OnPropertyChanged(nameof(Name));
}
}
#region INotifyPropertyChanged members
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Why would I chose version 1 over version 2 and vice-versa?
[CallerMemberName]
does which you explained as well :) – Communistic