✅ Disposable class level property
Hello
If there is a singleton service that uses a disposable class level property, should the service implement IDisposable as well?
Also, wouldn't the property be disposed automatically upon application shutdown, only if it implements IDisposable?
2 Replies
it's pretty common for classes to take ownership of their disposable dependencies yes
and they would then be responsible for disposing of them (normally by being disposable themselves)
it's generally a matter of what makes most sense for the lifetimes of the objects you're dealing with
if you're just passing an
IDisposable
as an argument to a method, it's generally expected the caller will still be responsible for managing its lifetime
if it's an argument in a constructor and your class would reasonably be expected to take ownership of the thing (i.e. you'd just be referencing the containing class at that point), then that'd be the way to go
there are no hard and fast rules and unfortunately there's no language constructs to support itThank you!