Skip to content

Instantly share code, notes, and snippets.

@techyrajeev
Forked from bugshake/ObservableProperty.cs
Created July 12, 2022 18:30
Show Gist options
  • Save techyrajeev/cbaf39181d4267cd6e620a1ca5013e0f to your computer and use it in GitHub Desktop.
Save techyrajeev/cbaf39181d4267cd6e620a1ca5013e0f to your computer and use it in GitHub Desktop.
C# Observable Property
// get an event when a property changes
public class ObservableProperty<T>
{
T value;
public delegate void ChangeEvent(T data);
public event ChangeEvent changed;
public ObservableProperty(T initialValue)
{
value = initialValue;
}
public void Set(T v)
{
if (!v.Equals(value))
{
value = v;
if (changed != null)
{
changed(value);
}
}
}
public T Get()
{
return value;
}
public static implicit operator T(ObservableProperty<T> p)
{
return p.value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment