使用-我的信息流发生了什么?

问题描述:

也许这是一个琐碎的问题,但这困扰着我。而且,如果重复的话也不要大喊大叫-我试图进行搜索,但是关于使用的问题太多,以至于我很难找到答案。

Maybe it is a trival question, but it's bothering me. And don't shout laud if it is a duplicate - I tried to search, but there are so many questions regarding using that it was hard for me to find the answer.

我有这样的代码:

using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("example.txt", FileMode.Create, ISF)))
     writeFile.WriteLine("Example");

我的问题是:创建的 IsolatedStorageFileStream $ c会怎样? $ c>,当处置 StreamWriter 时,如何使用?还会处置吗?

And my questions are: What happens to my created IsolatedStorageFileStream, when StreamWriter is disposed, while leaving using? Will it be also disposed?

与以下代码相比有什么区别:

Is there any difference in comparison to this code:

using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream stream = ISF.CreateFile("example.txt"))
using (StreamWriter writeFile = new StreamWriter(stream))
     writeFile.WriteLine("Example");

预先感谢。

您有一个 StreamWriter 的构造函数(仅适用于NET Framework 4.5),它允许指定 leaveOpen 布尔值,该布尔值定义了您的实例

You have a constructor for StreamWriter (NET Framework 4.5 only) that allows specifying the leaveOpen boolean that defines whether your instance takes ownership of the underlying stream or not.

如果未指定(例如,在您的示例中或框架的先前版本中),默认情况下为 false ,因此关闭(或处置)该实例会关闭基础流。

If not specified (as in your example, or for previous versions of the framework), by default it's false, so closing (or disposing) the instance closes the underlying stream.


除非您设置LeaveOpen如果参数为true,则在调用
StreamWriter.Dispose时,StreamWriter
对象将对提供的Stream对象调用Dispose()。

Unless you set the leaveOpen parameter to true, the StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

因此,您提供的这两段代码之间没有区别

So there is no difference between both pieces of code you provided.