Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:50:51+00:00 2026-05-25T18:50:51+00:00

Classes such as Stream , StreamReader , StreamWriter etc implements IDisposable interface. That means,

  • 0

Classes such as Stream, StreamReader, StreamWriter etc implements IDisposable interface. That means, we can call Dispose() method on objects of these classes. They’ve also defined a public method called Close(). Now that confuses me, as to what should I call once I’m done with objects? What if I call both?

My current code is this:

using (Stream responseStream = response.GetResponseStream())
{
   using (StreamReader reader = new StreamReader(responseStream))
   {
      using (StreamWriter writer = new StreamWriter(filename))
      {
         int chunkSize = 1024;
         while (!reader.EndOfStream)
         {
            char[] buffer = new char[chunkSize];
            int count = reader.Read(buffer, 0, chunkSize);
            if (count != 0)
            {
               writer.Write(buffer, 0, count);
            }
         }
         writer.Close();
      }
      reader.Close();
   }
}

As you see, I’ve written using() constructs, which automatically call Dispose() method on each object. But I also call Close() methods. Is it right?

Please suggest me the best practices when using stream objects. 🙂

MSDN example doesn’t use using() constructs, and call Close() method:

  • How to: Download Files with FTP

Is it good?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:50:52+00:00Added an answer on May 25, 2026 at 6:50 pm

    A quick jump into Reflector.NET shows that the Close() method on StreamWriter is:

    public override void Close()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }
    

    And StreamReader is:

    public override void Close()
    {
        this.Dispose(true);
    }
    

    The Dispose(bool disposing) override in StreamReader is:

    protected override void Dispose(bool disposing)
    {
        try
        {
            if ((this.Closable && disposing) && (this.stream != null))
            {
                this.stream.Close();
            }
        }
        finally
        {
            if (this.Closable && (this.stream != null))
            {
                this.stream = null;
                /* deleted for brevity */
                base.Dispose(disposing);
            }
        }
    }
    

    The StreamWriter method is similar.

    So, reading the code it is clear that that you can call Close() & Dispose() on streams as often as you like and in any order. It won’t change the behaviour in any way.

    So it comes down to whether or not it is more readable to use Dispose(), Close() and/or using ( ... ) { ... }.

    My personal preference is that using ( ... ) { ... } should always be used when possible as it helps you to “not run with scissors”.

    But, while this helps correctness, it does reduce readability. In C# we already have plethora of closing curly braces so how do we know which one actually performs the close on the stream?

    So I think it is best to do this:

    using (var stream = ...)
    {
        /* code */
    
        stream.Close();
    }
    

    It doesn’t affect the behaviour of the code, but it does aid readability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that classes can implement various special methods, such as __iter__ , __setitem__
I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public
I have several classes such as Order, Customer, etc. These classes serve for holding
Is it bad policy to have lots of work classes(such as Strategy classes), that
I have a few classes, such as those that outline database table structure or
Classes that implement IEnumerable and provide a public void Add(/* args */) function can
In the following question are the bytes handled by the Buffered classes such that
If I have a generic interface with a couple of implementing classes such as:
Previously, I used to use MFC collection classes such CArray and CMap . After
I have 3 abstract classes (Customer, Tender, Site). For Customer has inherited class such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.