Let's say I have two classes, and none of them are GUI components. Class A is a short-lived object that is logged for an event declared by long-lived object B. For example
public A(B b) { b.ChangeEvent += OnChangeEvent; }
If A never deregisters from a B event, will garbage be collected? Is a Dispose method required to exclude registration from event B?
There is also a second question. If A and B must live during the entire execution time of the application, is it necessary AAAAAAAAAAAAAB
: . B A. , A , B. , , . App.OnIdle.
: .
, , IDisposable.Dispose A. . A finalizer/destructor, A - B, B, ; , , , .
IDisposable.Dispose
A
B
, , , . , -, , . . , , . , , , , . "", , ; , .
B A, A , B .
You do not need the Dispose method for this. If B has no references pointing to this, the garbage collector will be smart enough to dispose of both B and A.
If they are both alive for the life of the application, you do not need to unregister the event.