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.
I've a code like this:
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("example.txt", FileMode.Create, ISF)))
writeFile.WriteLine("Example");
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");
Thanks in advance.
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.
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.
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.