嵌套“了IDisposable在一个单一的'使用'声明 [英] Nesting 'IDisposable's in a single 'using' statement
问题描述
有关在一个单一的使用嵌套一次性使用的语句快速的问题:我应该写出来的每一个一次性的using语句,或者我可以窝他们到一个?例如:
Quick question about using nested disposables in a single 'using' statement: Should I write out each disposable's using statement, or can I nest them into one? Example:
using( FileStream inFile = new FileStream( "myFile.txt", FileMode.Open ) )
using( GZipStream gzip = new GZipStream( inFile, CompressionMode.Decompress ) )
using( FileStream outFile = new FileStream( "myNewFile.txt", FileMode.CreateNew ) )
{
gzip.CopyTo( outstream );
}
VS
vs.
using( GZipStream gzip = new GZipStream( new FileStream( "myFile.txt", FileMode.Open ), CompressionMode.Decompress ) )
using( FileStream outFile = new FileStream( "myNewFile.txt", FileMode.CreateNew ) )
{
gzip.CopyTo( outstream );
}
只是好奇,如果当块完成从MYFILE执行,无名的FileStream 。.TXT被清理,因为它是与GZipStream using语句,或者如果它保持打开状态,需要以后的某个时候清理
Just curious if when the block is done executing, the unnamed FileStream from "myFile.txt" gets cleaned up because it's in the using statement with the GZipStream or if it stays open and needs to be cleaned up sometime after that.
编辑:
只是要清楚,我不是问嵌套using语句。我询问是否是在另一个了IDisposable的'使用'语句创建一个IDisposable接口将在块的末尾进行处置。为什么或者为什么不将不胜感激任何解释。
Just to be clear, I'm not asking about nesting using statements. I'm asking whether or not an IDisposable that is created inside another IDisposable's 'using' statement will be disposed of at the end of the block. Any explanation on why or why not would be appreciated.
推荐答案
这取决于构造, GZipStream <流/ code>部署在处置它,除非你使用的重载它接受一个布尔值和你传递
真正
到 leaveOpen
。
It depends on the constructor, GZipStream
disposes of the stream you passed in when you dispose of it unless you use one of the overloads that takes in a bool and you pass in true
to leaveOpen
.
不过你运行的风险这样做。如果 GZipStream
抛出一个的ArgumentException
因为在的CanRead
属性流是假
在流传递没有得到处理。
However you do run a risk doing this. If GZipStream
throws a ArgumentException
because the CanRead
property of the stream is false
the passed in stream does not get disposed of.
我个人而不是靠事不。脚麻,而是通常的代码防守,并使用3报表版本
Personally I rather not depend on "something not going wrong" and instead usually code defensively and use the 3 statement version.
编辑:只是要清楚,我不是问嵌套using语句。
我询问是否是$ B $内部创建b另一IDisposable的的使用声明的IDisposable接口将在块的末尾
进行处理。 。为什么或者为什么不将不胜感激任何解释
Just to be clear, I'm not asking about nesting using statements. I'm asking whether or not an IDisposable that is created inside another IDisposable's 'using' statement will be disposed of at the end of the block. Any explanation on why or why not would be appreciated.
如果这是你的问题,那么答案是:没有,只有对象申报已分配给(使用VAR任何= ...
)将被全部销毁,创建的任何其他对象依赖于任何的外目的是执行代码以连锁称之为的Dispose()
方法。
If that is your question then the answer is: No, only the object declared that was assigned to (using var whatever = ...
) will be disposed, any other objects created are dependent on the code of whatever the "outer" object is to be implemented to "chain call" the Dispose()
methods.
这篇关于嵌套“了IDisposable在一个单一的'使用'声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!