C
C#2mo ago
Core

✅ 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?
c#
public class IpLookupService : IIpLookupService
{
private readonly Reader _reader; // This property can be disposed
}
c#
public class IpLookupService : IIpLookupService
{
private readonly Reader _reader; // This property can be disposed
}
2 Replies
surf68
surf682mo ago
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 it
Core
CoreOP2mo ago
Thank you!

Did you find this page helpful?