It's important to mention that the question is about an owned object which its lifecycle will be controlled by the container it is declared in.
For an external object which has been assigned from outside which lifecycle is not controlled by the container class, it is neccessary to unsubscribe its event handler when the class will be disposed/destroyed to avoid memory leaks.
public class ContainerClass: IDisposable
{
private INotifyPropertyChanged SomeNotifyObject = new SomeNotifyPropertyChangeedImplementingClass();
public ContainerClass()
{
SomeNotifyObject.PropertyChanged += SomeNotifyObjectOnPropertyChanged;
}
// Neccessary if ContainerClass OWNS the subscribed object?
void IDisposable.Dispose()
{
SomeNotifyObject.PropertyChanged -= SomeNotifyObjectOnPropertyChanged;
}
private void SomeNotifyObjectOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Stuff
}
}
If the anser is no, it is not neccessary in case of an owned object, code could be written less complex:
private INotifyPropertyChanged SomeNotifyObject = SomeNotifyPropertyChangeedImplementingClass();
public ContainerClass()
{
SomeNotifyObject.PropertyChanged += (sender, args) =>
{
/* Call whatever you want */
};
}
}