Home
> Uncategorized > How to Implement a Property
How to Implement a Property
Below is a code snippet of how we implement non-trivial properties, involving event subscriptions and change notifications (through INotifyPropertyChanged). Unsubscribing from the old value’s events is particularly important to avoid memory leaks.
public Amount BatchAmount { set { // Do nothing if the value hasn't changed (certainly avoid change notification) if (batchAmount == value) return; // Unsubscribe from the old value's events (prevent memory leak) if (batchAmount != null) batchAmount.PropertyChanged -= batchAmount_PropertyChanged; // Assign the new value batchAmount = value ?? new Amount(0, UnitPrice.Unit); // Subscribe on the new value's events batchAmount.PropertyChanged += batchAmount_PropertyChanged; // Notify property changed OnPropertyChanged("BatchAmount"); } }
Categories: Uncategorized
.NET
Comments (0)
Trackbacks (0)
Leave a comment
Trackback