0

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 */
        };
    }
}
CleanCoder
  • 2,406
  • 1
  • 10
  • 35
  • Less complex? I don´t see much difference, expect that you´re using an anonymous function instead of a named one. – MakePeaceGreatAgain Mar 14 '20 at 16:36
  • SomeNotifyObject keeps a reference to the container through the event subscription. Not visible, but surely the container keeps a reference to SomeNotifyObject. Circular references are not a problem to the GC. In most any practical scenario, the container needs to live longer than the objects it contains. So no, as long as you remove the object from the container then you're good without having to unsubscribing the event. – Hans Passant Mar 14 '20 at 16:53
  • HomBromBeere. No need for implementing IDisposable or using an destructor. half of the code lines removed I call "less complex". – CleanCoder Mar 14 '20 at 17:32
  • See marked duplicate. Whether you need to unsubscribe depends on which object you expect to become unreachable. Circular references don't matter. If the only object referencing the object with the event handler, is the only object that actually has a reference to the object that published the event, and the owner object becomes unreachable, then the publishing object also becomes unreachable, and both can be collected. Without a good [mcve] it's impossible to say for sure in _your_ example whether that's the case; just apply the usual, very simple rules about reachability. – Peter Duniho Mar 14 '20 at 21:10
  • the minimal example you can see above. – CleanCoder Mar 14 '20 at 22:37

1 Answers1

0

In general, you don't need to explicitly remove event listeners attached to nested objects. Yes, you can use an anonymous method function of declaring a named method in that case.

It can even get a little simpler if you're not doing much in the callback.

SomeNotifyObject.PropertyChanged += (s, e) => textBox.Text = e.Message;
Pranav Negandhi
  • 1,594
  • 1
  • 8
  • 17