none
Code not working without 'using' clause RRS feed

  • Question

  • Hello!

    I'm developing a WPF application in which I need to compress/decomperss Gzip files. I referred to this MSDN article and was able to decompress properly without the 'using' clause used throughout that code, since I don't exactly understand why to use it. But compression is not working without the 'using' clause.

    My code without 'using" clause:

    private void cbutton_Click(object sender, RoutedEventArgs e)
    {
    FileInfo filetocompress = new FileInfo("C:\\Users\\prate_000\\Downloads\\icsharpcode-SharpZipLib-9478d50.tar");
    FileStream originalfilestream = filetocompress.OpenRead();
    FileStream compfilestream = File.Create(filetocompress.FullName + ".gz");
    GZipStream compstream = new GZipStream(compfilestream, CompressionMode.Compress);
    originalfilestream.CopyTo(compstream);
    }

    This is generating a proper .gz file, but when I use 7-zip to open the contents of this .gz file (which is .tar file), it says data is broken. While when I use this code:

            private void cbutton_Click(object sender, RoutedEventArgs e)
            {
                FileInfo filetocompress = new FileInfo("C:\\Users\\prate_000\\Downloads\\icsharpcode-SharpZipLib-9478d50.tar");
                using (FileStream originalfilestream = filetocompress.OpenRead())
                {
                    using (FileStream compfilestream = File.Create(filetocompress.FullName + ".gz"))
                    {
                        using (GZipStream compstream = new GZipStream(compfilestream, CompressionMode.Compress))
                        {
                            originalfilestream.CopyTo(compstream);
                        }
                    }
                }
            }

    the compression is proper and I'm able to open the contents (the .tar) in 7-zip properly.

    I wanted to know, what exactly is the 'using' clause doing, which my code without 'using' clause fails at? And then why is my decompression code working even after eliminating 'using' clause.

    According to the using Statement (C# Reference) article, 'using' clause automatically calles the Dispose method. So is it same as me manually calling the FileStream.Close() methods for each FileStream object in my code without 'using' clause?

    Any other advantages of 'using' clause that I'm not aware of? Till now I used 'using' statement only for referencing namespaces (using System.IO, so on). Hence I'm quite uncomfortable using the 'using' statement in the middle of my code, which is why I'm avoiding it.

    Thank you!


    - Prateek Jain

    Tuesday, March 10, 2015 10:10 AM

Answers

  • >>I wanted to know, what exactly is the 'using' clause doing

    It is calling the Dispose() method of the object that is wrapped in the using statement implicitly.

    >>So is it same as me manually calling the FileStream.Close() methods for each FileStream object in my code without 'using' clause?

    It is the same thing as manually calling the Dispose() method on each object. You could simpy call the Dispose() methods explicitly yourself when not using the using statement:

            private void cbutton_Click(object sender, RoutedEventArgs e)
            {
                FileInfo filetocompress = new FileInfo("C:\\Users\\prate_000\\Downloads\\icsharpcode-SharpZipLib-9478d50.tar");
                FileStream originalfilestream = filetocompress.OpenRead();
                FileStream compfilestream = File.Create(filetocompress.FullName + ".gz");
                GZipStream compstream = new GZipStream(compfilestream, CompressionMode.Compress);
                originalfilestream.CopyTo(compstream);
                compfilestream.Dispose();
                compfilestream.Dispose();
                originalfilestream.Dispose();
            }

    >>Any other advantages of 'using' clause that I'm not aware of?

    No, it simply calls the Dispose() method of the object for you when it goes out of scope. It is nothing more than a convenience thing.

    >>Till now I used 'using' statement only for referencing namespaces (using System.IO, so on).

    That's the using directive which is a totally different thing.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

    • Marked as answer by Prateek.Jain Tuesday, March 10, 2015 4:30 PM
    Tuesday, March 10, 2015 3:01 PM

All replies

  • I think that you should take into consideration the differences between using directive (e.g. using System.IO), which simplifies the text of the program without functional effects, and the using statement, shown in your sample, which automatically releases the resources, such as files and streams.

    Your using statements will automatically close the input and output files, therefore you do not have to call Close manually. The system will execute Close even if your function has a complex structure or multiple return statements or even if some errors (exceptions) occur in the middle of the operations. Otherwise, if you forgot to call Close manually, then the files become “locked” for some period of time, which produces problems if you try to access them again. It is also possible to lose the data in some circumstances.




    • Edited by Viorel_MVP Tuesday, March 10, 2015 11:35 AM
    Tuesday, March 10, 2015 11:32 AM
  • >>I wanted to know, what exactly is the 'using' clause doing

    It is calling the Dispose() method of the object that is wrapped in the using statement implicitly.

    >>So is it same as me manually calling the FileStream.Close() methods for each FileStream object in my code without 'using' clause?

    It is the same thing as manually calling the Dispose() method on each object. You could simpy call the Dispose() methods explicitly yourself when not using the using statement:

            private void cbutton_Click(object sender, RoutedEventArgs e)
            {
                FileInfo filetocompress = new FileInfo("C:\\Users\\prate_000\\Downloads\\icsharpcode-SharpZipLib-9478d50.tar");
                FileStream originalfilestream = filetocompress.OpenRead();
                FileStream compfilestream = File.Create(filetocompress.FullName + ".gz");
                GZipStream compstream = new GZipStream(compfilestream, CompressionMode.Compress);
                originalfilestream.CopyTo(compstream);
                compfilestream.Dispose();
                compfilestream.Dispose();
                originalfilestream.Dispose();
            }

    >>Any other advantages of 'using' clause that I'm not aware of?

    No, it simply calls the Dispose() method of the object for you when it goes out of scope. It is nothing more than a convenience thing.

    >>Till now I used 'using' statement only for referencing namespaces (using System.IO, so on).

    That's the using directive which is a totally different thing.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

    • Marked as answer by Prateek.Jain Tuesday, March 10, 2015 4:30 PM
    Tuesday, March 10, 2015 3:01 PM
  • Oh, I see. Yeah my final output were locked, but I thought it could open in 7-zip since it's only reading from the file and don't need to write it. Wonder why it says data is broken.

    Thanks for answering :)


    - Prateek Jain

    Tuesday, March 10, 2015 4:28 PM
  • Oh. Thank you! I understand it better now. I'll go with 'using' clause, and let the computer do all the disposing part :)


    - Prateek Jain

    Tuesday, March 10, 2015 4:30 PM