IDataReader::Dispose 应该调用 IDataReader::Close 吗?
我正在实现 IDataReader
,我想知道 Dispose
的实现是否应该调用 Close
。
另外,Close
应该调用Dispose
吗?
我的猜测是 Close
不应调用 Dispose
而 Dispose
可以 调用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Close()
和Dispose()
应该做同样的事情(一个应该调用另一个)。但是,
Close()
不需要多次调用。您应该继承 DbDataReader 基类,而不是自己重新实现整个内容;如果是这样,您需要重写抽象
Close()
。Close()
andDispose()
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 abstractClose()
..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.它们几乎是一样的,除了 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().
IDataReader
位于界面中。它并不规定实施。IDataReader
is in interface. It doesn't dictate implementation.