也许这是一个棘手的问题,但它困扰着我。如果它是重复的,不要大声赞扬 - 我试图搜索,但是有很多关于使用的问题,我很难找到答案。使用 - 我的流发生了什么?
我有这样的代码:
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("example.txt", FileMode.Create, ISF)))
writeFile.WriteLine("Example");
而且我的问题是:会发生什么我创建IsolatedStorageFileStream
,当StreamWriter
配置,同时采用要走了?它会被处置吗?
是否有与此相比代码的任何区别:
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream stream = ISF.CreateFile("example.txt"))
using (StreamWriter writeFile = new StreamWriter(stream))
writeFile.WriteLine("Example");
在此先感谢。
@AdamHouldsworth StreamWriter本身在底层的流上调用Dispose,然后在流上调用Close。所以,是的,如果底层流实现覆盖'Close'到_not_实际关闭它不会工作流,但它是那么恕我直言,这是不值得被考虑到文档中的一个_silly_实现。 – ken2k
@AdamHouldsworth,ken2k(亚当 - 我不知道为什么你已经删除回答)是的,我看到这两个答案,他们是根据问题都正确(现在是我只有一个节拍一个问题:|并没有更多的票)。总结一下,如果我编写自己的IDisposable并且不处理正确的Dispose,那么创建的流可以存活,除非它被GC收集? – Romasz
@Romasz GC和Dispose实际上是两个不同的东西。 GC最终会清理您的代码中不再引用的实例; Dispose是用于立即释放(非托管)资源的手动调用。 “我自己的IDisposable”是什么意思? – ken2k