c# - Why is the stream left open in this code? -


I have a question about a piece of code from the book: C # Exam 70-483 in the programming code is here:

  WebRequest requests = WebRequest.Create ("http://www.microsoft.com"); WebResponse response = Request GetResponse (); Streamer feedbackflow = new streamerdidder (feedback. Gatesresponsescrew ()); String feedback text = response light Readout (); Console.WriteLine (responseText); // displays HTML of the website. Stop it ();  

My question is why does the feedback stream stop in this example? Why is the feedback object closed only? As NSGOSW said, Docs says that the call to the web resolution stops the underlying current. Then there is no point in trying to close it.

But I can not help answering and can not take into account that in the example code processing does not do anything to save itself from the leak. If the exception occurs, whether in your code or in communication with remote web servers, the feedback will not stop and the underlying stream will not be released, which means that TCP socket will probably not be released.

At the very least, using the code "refrigerator" to protect the self and guarantees , which are the resources released:

 Using the  (WebRequest request = WebRequest.Create ("http://www.microsoft.com")) ({WebResponse response = request.GetResponse ()) {StreamReader responseStream = New Streamerdream (Response .GetrespanSstream () ); String feedback text = response light Readout (); Console; light line (feedback text); // displays HTML of the website}}  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -