1

I often use the "using" block to dispose the objects. Today, I using HttpWebRequest to post data, and I feel confused between two method.

Method 1:

var request = (HttpWebRequest)WebRequest.Create("http://www...");
using (var writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write(JsonConvert.SerializeObject(content));
}

Method 2:

var request = (HttpWebRequest)WebRequest.Create("http://www...");
using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
{
    writer.Write(JsonConvert.SerializeObject(content));
}

In method 2, all stream and streamwirter in "using" blocks, so certainly it will be dispose. But in method 1, I am not sure stream request.GetRequestStream() will be dispose. Can anyone explain to me? Thanks alot!

3 Answers 3

9

Basically, it depends on two things:

  • Whether the StreamWriter constructor will ever throw an exception when passed a non-null Stream reference - and I don't think it will in this case. (If the stream were read-only, it would... at which point the stream wouldn't be disposed.)
  • Whether StreamWriter.Dispose disposes of the underlying stream - and it does, unless you've called the constructor overload which explicitly allows you to control that

So basically, I believe it's safe in this case... when your using statement disposes of the StreamWriter, that will dispose of the Stream.

As noted in comments, however, using two using statements means you don't need to perform this sort of reasoning.

Sign up to request clarification or add additional context in comments.

4 Comments

So, method 1 is as safe as method 2?
@MinhGiang: Yes, exactly.
Are you sure about that? If Stream.CanWrite is false for whatever reason (something unpredictable happens with request) then StreamWriter constructor will throw ArgumentException and underlying stream might end up not being disposed. Not saying its likely to happen but want to point out that some practices (like method 1) force you to think about particular implementations (which also might change in future, of course) and some practices don't. That's the reason why i would personally always choose method 2.
@deezg: That's a good point; I think it's okay in this case but I'll update the post.
5

A StreamWriter wrapping a Stream will close that stream when it is closed. See the docs.

Comments

-2

Both are the same. Since using block disposes the entire object, even anonymous objects.

2 Comments

No, they're not the same. The using statement only calls Dispose on the resources specified in the first part - this code is only okay because StreamWriter calls Dispose on the Stream that's passed into the constructor.
That seems to be true. I have read msdn.microsoft.com/es-es/library/…. Thank you

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.