Why is it always necessary to implement IDisposable on an object that has an IDisposable member?(为什么总是需要在具有 IDisposable 成员的对象上实现 IDisposable?)
问题描述
据我所知,如果你有一个类 A 有一个成员 m 是 IDisposable,A 应该实现 IDisposable 并且它应该在其中调用 m.Dispose() 是一个公认的规则.
From what I can tell, it is an accepted rule that if you have a class A that has a member m that is IDisposable, A should implement IDisposable and it should call m.Dispose() inside of it.
我找不到令人满意的理由.
I can't find a satisfying reason why this is the case.
我理解如果你有非托管资源,你应该提供一个终结器以及 IDisposable 的规则,这样如果用户没有显式调用 Dispose,终结器仍然会在 GC 期间清理.
I understand the rule that if you have unmanaged resources, you should provide a finalizer along with IDisposable so that if the user doesn't explicitly call Dispose, the finalizer will still clean up during GC.
但是,有了该规则,您似乎不需要拥有该问题所涉及的规则.比如……
However, with that rule in place, it seems like you shouldn't need to have the rule that this question is about. For instance...
如果我有课:
class MyImage{
private Image _img;
... }
约定规定我应该有 MyImage : IDisposable
.但是如果Image遵循约定,实现了finalizer,我不关心资源的及时释放,那还有什么意义呢?
Conventions states that I should have MyImage : IDisposable
. But if Image has followed conventions and implemented a finalizer and I don't care about the timely release of resources, what's the point?
更新
在 这里.
推荐答案
但如果 Image 遵循约定并实现了一个终结器,我不在乎及时发布资源,有什么用?
But if Image has followed conventions and implemented a finalizer and I don't care about the timely release of resources, what's the point?
那么没有一个,如果你不关心及时释放,你可以确保一次性对象写得正确(事实上我从来没有做过这样的假设,即使是 MSs 代码.你从来没有知道什么时候有东西意外溜过).关键是你应该关心,因为你永远不知道它什么时候会导致问题.考虑一个开放的数据库连接.让它闲置,意味着它不会在池中被替换.如果您有多个请求进入一个,您可能会用完.
Then there isn't one, if you don't care about timely release, and you can ensure that the disposable object is written correct (in truth I never make an assumption like that, not even with MSs code. You never know when something accidentally slipped by). The point is that you should care, as you never know when it will cause a problem. Think about an open database connection. Leaving it hanging around, means that it isn't replaced in the pool. You can run out if you have several requests come in for one.
没有什么说如果你不在乎就必须这样做.这样想,就像在非托管程序中释放变量一样.您不必这样做,但这是非常可取的.如果没有其他原因,从程序继承的人不必想知道为什么它没有得到照顾,然后尝试清理它.
Nothing says you have to do it if you don't care. Think of it this way, it's like releasing variables in an unmanaged program. You don't have to, but it is highly advisable. If for no other reason the person inheriting from the program doesn't have to wonder why it wasn't taken care of and then try and clear it up.
这篇关于为什么总是需要在具有 IDisposable 成员的对象上实现 IDisposable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么总是需要在具有 IDisposable 成员的对象上实现 IDisposable?
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30