1

I have the method :

public static void Main()
{
    string path = @"C:\Temp\ProgrammingInCSharp\DirectoryInfo\111.txt";
    using (FileStream fileStream = File.Create(path))
    {
        using (BufferedStream bufferedStream = new BufferedStream(fileStream))
        {
            using (StreamWriter streamWriter = new StreamWriter(bufferedStream))
            {
                streamWriter.WriteLine("A line of text.");
            }
        }
    }
}

Which is using 3 using statements and method works perfectly : create 111.txt and write "A line of text." inside it. But when I change the method to :

public static void Main()
{
    string path = @"C:\Temp\ProgrammingInCSharp\DirectoryInfo\111.txt";
    FileStream fileStream = File.Create(path);
    BufferedStream bufferedStream = new BufferedStream(fileStream);
    StreamWriter streamWriter = new StreamWriter(bufferedStream);
    streamWriter.WriteLine("A line of text.");
}

It just creates 111.txt file but doesn't write "A line of text." inside.

I cannot understand why.

As I read using Statement just call Dispose() method when the object leaves the scope of using Statement. So it should be used to dispose unmanaged code from CLR but why without using Statement I cannot write the text message to my machine's file?

4
  • 3
    using doesnt just Dispose, it also closes it - something code block #2 doesnt do Commented Nov 5, 2018 at 14:58
  • 2
    It's likely that because the stream is neither implicitly or explicitly closed it is being abandoned. You should probably add a streamWriter.Flush() Commented Nov 5, 2018 at 14:58
  • using statements effectively call the dispose method of an object that implements IDispoable. What that dispose method does it up to the implementing object. Yes disposing unmanaged resources is usually involved, but it can do other things, for example, closing a stream Commented Nov 5, 2018 at 14:59
  • @Disaffected1070452 If you post your comment as an answer, I will give you an upvote. Commented Nov 5, 2018 at 15:00

1 Answer 1

4

There is caching going on in the background: WriteLine writes to a cache, not directly to the file. So if the file isn't closed properly, the cache doesn't actually get written to the disk. The using statement disposes the object, which flushes the cache into the file and closes the file.

You can see the source code for StreamWriter.Dispose() here (notice that it calls Flush()): https://referencesource.microsoft.com/#mscorlib/system/io/streamwriter.cs,236

Note that with multiple using statements, you only need one code block. The effect is the same, but it's just easier to read.

using (FileStream fileStream = File.Create(path))
using (BufferedStream bufferedStream = new BufferedStream(fileStream))
using (StreamWriter streamWriter = new StreamWriter(bufferedStream))
{
    streamWriter.WriteLine("A line of text.");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice Explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.