IDataReader::Dispose 应该调用 IDataReader::Close 吗?

发布于 2024-12-21 09:51:25 字数 332 浏览 6 评论 0原文

我正在实现 IDataReader ,我想知道 Dispose 的实现是否应该调用 Close

另外,Close 应该调用Dispose 吗?

我的猜测是 Close 不应调用 DisposeDispose 可以 调用 Close因为据我所知,您应该能够在任何对象中多次调用 Dispose 。但这只是猜测,我更想听听专家的意见。

I'm implementing IDataReader and I wonder if the implementation of Dispose is supposed to call Close or not.

Also, should Close call Dispose?

My guess is that Close shouldn't call Dispose and Dispose can call Close since AFAIK you should be able to call Dispose as many times as you want in any object. But this is just a guess and I'd rather hear an expert's opinion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

成熟稳重的好男人 2024-12-28 09:51:25

Close()Dispose() 应该做同样的事情(一个应该调用另一个)。
但是,Close() 不需要多次调用。

您应该继承 DbDataReader 基类,而不是自己重新实现整个内容;如果是这样,您需要重写抽象 Close()

Close() and Dispose() should do the same thing (one should call the other).
However, Close() does not need to be callable multiple times.

You should inherit the base DbDataReader class instead of re-implementing the whole thing yourself; if so, you'll need to override the abstract Close().

生死何惧 2024-12-28 09:51:25

.Dispose() 应该只做一件事:释放非托管资源。因此,当且仅当调用该函数是释放非托管资源的正确方法时,您才应该调用 .Close()。在这种情况下,我想说这是真的(它将释放数据库连接),因此您应该调用该函数。

.Dispose() should do exactly one thing: release unmanaged resources. Therefore you should call .Close() if and only if calling that function is the correct way to release an unmanaged resource. In this case, I would say that is true (it will release a database connection), and so you should call the function.

妳是的陽光 2024-12-28 09:51:25

它们几乎是一样的,除了 Close 可以被调用超过 1 次而不会出现异常。

如果多次使用该对象,请实现 .Close,如果只使用一次,则实现 .Dispose()。

They are both almost the same thing except Close can be called more than 1 time without getting an exception.

If you use the object more than once, implement .Close, if only once, implement .Dispose().

毁梦 2024-12-28 09:51:25

IDataReader 位于界面中。它并不规定实施。

IDataReader is in interface. It doesn't dictate implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文