Skip to content

Instantly share code, notes, and snippets.

@tazlord
Created May 14, 2025 02:47
Show Gist options
  • Save tazlord/e5e8bc8cd993a963304681c50ecb1cd8 to your computer and use it in GitHub Desktop.
Save tazlord/e5e8bc8cd993a963304681c50ecb1cd8 to your computer and use it in GitHub Desktop.
An abstract C# class that allows subscribers of derived classes to be notified of property value changes.
using System.ComponentModel;
using System.Runtime.CompilerServices;
/// <summary>
/// Represents an object that notifies subscribers of property value changes.
/// Implements the <see cref="INotifyPropertyChanged"/> interface.
/// </summary>
public abstract class ObservableObject : INotifyPropertyChanged
{
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event for the specified property.
/// </summary>
/// <param name="propertyName">
/// The name of the property that changed.
/// This value is automatically supplied by the compiler when called from the property setter.
/// </param>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null!)
{
if (PropertyChanged != null)
{
var e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment