IMHO, the PostSharp approach, as in the accepted answer, is very nice and is of course the direct answer to the question asked.
However, for those who can't or won't use a tool like PostSharp to extend the C# language syntax, one can get most of the benefit of avoiding code repetition with a base class that implements INotifyPropertyChanged
. There are many examples lying around, but none have so far been included in this useful and well-trafficked question, so here is the version I generally use:
/// <summary>
/// Base class for classes that need to implement <see cref="INotifyPropertyChanged"/>
/// </summary>
public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
/// <summary>
/// Raised when a property value changes
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Updates a field for a named property
/// </summary>
/// <typeparam name="T">The type of the field</typeparam>
/// <param name="field">The field itself, passed by-reference</param>
/// <param name="newValue">The new value for the field</param>
/// <param name="onChangedCallback">A delegate to be called if the field value has changed. The old value of the field is passed to the delegate.</param>
/// <param name="propertyName">The name of the associated property</param>
protected void UpdatePropertyField<T>(ref T field, T newValue,
Action<T> onChangedCallback = null,
[CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, newValue))
{
return;
}
T oldValue = field;
field = newValue;
onChangedCallback?.Invoke(oldValue);
OnPropertyChanged(propertyName);
}
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event.
/// </summary>
/// <param name="propertyName">The name of the property that has been changed</param>
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Used, for example, like this:
private int _value;
public int Value
{
get { return _value; }
set { UpdatePropertyField(ref _value, value); }
}
Not quite as concise as being able to just apply a code attribute to an auto-implemented property as in the PostSharp approach, but still goes a long way to speeding the implementation of view models and other similar types.
The key features above that distinguish it from some other implementations:
- Equality is compared using
EqualityComparer<T>.Default
. This ensures that value types can be compared without being boxed (a common alternative would be object.Equals(object, object)
). The IEqualityComparer<T>
instance is cached, so after the first comparison for any given type T
, it's very efficient.
- The
OnPropertyChanged()
method is virtual
. This allows derived types to easily and efficiently handle property changed events in a centralized way, without having to subscribe to the PropertyChanged
event itself (e.g. for multiple levels of inheritance) and also of course gives the derived type better control over how and when it handles the property changed event relative to raising the actual PropertyChanged
event.
if ( m_Fieldname != value ) {...}
? It is more code, I know, but raisingPropertyChanged
doesn't seem right if the property doesn't change.