using语句上的一些先进的问题 [英] Some advanced questions on the using statement

查看:123
本文介绍了using语句上的一些先进的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一些关于这里如何使用using语句,并调用Dispose()方法的线程。我已经阅读了大部分这些线程。

如果我调用Dispose(),它调用Close()?

如果我想使用一个对象(比如SqlDataReader的),但随后又用它在另一个code座,我不应该调用Dispose()?这也意味着省略using语句。

此外,为了澄清,如果一个FileStream被包装使用StreamWriter和我调用Dispose因此FileStream,这将调用flush(),关闭()和Dispose()方法(根据的Dispose()是否调用Close())上该StreamWriter的,对不对?同样,如果我叫的关闭的FileStream,这样只会调用flush()和close()上的FileStream。

时检查白细胞介素的好办法回答关于正在发生的事情引擎盖下这些问题呢?

解决方案
  

如果我调用Dispose(),它调用Close()?

调用 处置 应该采取任何必要的行动来处置资源,这应该是相似,如果不完全相同的,叫的 关闭 。然而,这是一个实现细节并不一定保证的(虽然我们可以预期,BCL遵循本准则)。

  

如果我想使用一个对象(比如SqlDataReader的),但随后又用它在另一个code座,我不应该调用Dispose()?这也意味着省略using语句。

如果您想再次使用该对象,你绝对不应该丢弃它。然而,你通常应该使用两个单独的连接,如果你要在数据库中的两个不同的时间。这通常不是一个好主意,让一个的IDataReader 各地一个长于是需要抓住你需要的数据。

  

此外,为了澄清,如果一个FileStream被包装使用StreamWriter和我调用Dispose因此FileStream,这将调用flush(),关闭()和Dispose()方法(根据的Dispose()是否调用Close())上该StreamWriter的,对不对?同样,如果我叫的关闭的FileStream,这样只会调用flush()和close()上的FileStream。

处置的对象,包装另一个一次性对象应该叫处置内部对象。呼叫关闭的FileStream 将在调用它的处置方法好了,所以它也将作用于两个流。

  

时检查白细胞介素的好办法回答关于正在发生的事情引擎盖下这些问题呢?

检查白细胞介素会肯定地回答大部分问题明确。作为<一个href="http://stackoverflow.com/questions/667111/some-advanced-questions-on-the-using-statement/667123#667123">@Rich说,你也可以只是尝试调试自己的Dispose实现。还有,当然,MSDN文档入手,然后再尝试搞清楚自己,反射器,如果你不想周围的淤泥中IL。

I know there are a number of threads on here about how to use the using statement and calling the Dispose() method. I have read the majority of these threads.

If I call Dispose(), does it call Close()?

If I want to use an object (say SqlDataReader), but then use it again in another code block, should I not call Dispose()? Which also means to omit the using statement.

Also, to clarify, if a FileStream is wrapping a StreamWriter and I call dispose on the FileStream, this will call Flush(), Close() and Dispose() (depending on whether Dispose() calls Close()) on the StreamWriter, right? Likewise, if I call Close on the FileStream, this will only call Flush() and Close() on the FileStream.

Is checking IL a good way to answer these questions about what is happening under the hood?

解决方案

If I call Dispose(), does it call Close()?

Calling Dispose should take any required actions to dispose of the resource, which should be similar, if not identical to, calling Close. This, however, is an implementation detail and not necessarily guaranteed (though we can expect that the BCL follows this guideline).

If I want to use an object (say SqlDataReader), but then use it again in another code block, should I not call Dispose()? Which also means to omit the using statement.

If you want to use the object again, you definitely should not dispose it. However, you should typically use two separate connections if you're going to the database two separate times. It's generally not a good idea to keep an IDataReader around an longer than is needed to grab your needed data.

Also, to clarify, if a FileStream is wrapping a StreamWriter and I call dispose on the FileStream, this will call Flush(), Close() and Dispose() (depending on whether Dispose() calls Close()) on the StreamWriter, right? Likewise, if I call Close on the FileStream, this will only call Flush() and Close() on the FileStream.

Disposing an object that wraps another disposable object should call Dispose on the interior object. Calling Close on a FileStream will call its Dispose method under the good, so it will also act on both streams.

Is checking IL a good way to answer these questions about what is happening under the hood?

Checking IL will definitely answer most of these questions definitively. As @Rich says, you can also just try debugging your own Dispose implementations. There is also, of course, MSDN documentation to start with before you try to figure it out yourself, and Reflector if you don't want to muck around in IL.

这篇关于using语句上的一些先进的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆