Created
May 14, 2025 02:47
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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