4

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.

Romasz
  • 29,662
  • 13
  • 79
  • 154

4 Answers4

5

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.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • @AdamHouldsworth The `StreamWriter` itself calls `Dispose` on the underlying stream, which in turn calls `Close` on the stream. So yes, if the underlying stream implementation overrides `Close` to _not_ actually close the stream it wouldn't work, but it would be a _silly_ implementation so IMHO it's not worth being taken into account in the documentation. – ken2k Feb 18 '14 at 09:52
  • @AdamHouldsworth , ken2k (Adam - I'm not sure why you have deleted answer) Yes I see both answers and they are both correct according to the question (now there is a problem that I have only one tick :| and no more votes up). To sum up if I write my own IDisposable and do not handle Dispose correctly, my created stream can survive unless it's collected by GC? – Romasz Feb 18 '14 at 10:09
  • @Romasz GC and Dispose are two different things actually. GC eventually cleans-up instances that are not referenced anymore in your code; Dispose is a manual call that is used to immediately release (unmanaged) resources. What do you mean by "my own IDisposable"? – ken2k Feb 18 '14 at 10:13
0

Once it leaves the using block, Dispose is called.

using Statement (C# Reference)

The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object.

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
0

Your stream remains open even though the stream writer is disposed of. You could, for example, open another stream writer and continue writing to the stream.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
-1

Use {} always! It makes the intention of your code a lot better. Your code looks like:

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

You can then see that the StreamWriter is executed in the context of the ISF. If I understand the ISF correctly, the ISF should not be closed when the Streamwriter closes the file. And you could open another File in the ISF Block.

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85