Say I have two classes, and neither of them are GUI components. Class A is a short lived object that registers for an event declared by a long lived object B. For example
public A(B b)
{
b.ChangeEvent += OnChangeEvent;
}
If A never deregisters from B’s event, will A never be garbage collected? Does A need a Dispose method just to deregister from B’s event?
There is also a related second question. If A and B should both live for the entire execution time of the application, does A need to deregister?
To your first question: Yes. B has a reference to A. That way A will live as long as B. This is a nice way to loose memory in a UI app if you e.g. register with an event like App.OnIdle.
To the second: At the end everything will be killed.