E.g. for a general type, which subscribe to some events in constructor:
class SomeType
{
public SomeType(...)
{
someEvent1 += ...
someEvent2 += ...
}
}
Where do I unsubscribe from events?
- Finalizer?
IDisposable
?- Some method
DontForgetToCallMeSoICanUnsubscribeFromEvents()
? - Use weak events pattern?
I know it depends. In case of controls (wpf, winforms) there are some events what can be used to subscribe/unsubscribe like Loaded
/Unloaded
, HandleCreated
/HandleDestroyed
, etc. But what if parent is a simple object
?
And some more specific example: nested ViewModels, where each level is a List<NextLevelVM>
, at any level ViewModel can be deleted, does that means what each ViewModel must implement IDisposable
(if e.g. it is the right way) where it call Dispose
for each item in their list? I tried to use weak events, but that doesn't go well.