When sending an HTTP request, the following code is used (the code is taken from MSDN and wrapped with using operators). How to avoid multiple nesting of operators?

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { result = reader.ReadToEnd(); reader.Close(); } stream.Close(); } response.Close(); } 

    3 answers 3

    In using you can specify multiple statements separated by commas. But it requires that they be of the same type. something will turn out like:

     using (IDisposable response = (HttpWebResponse)request.GetResponse(), IDisposable stream = response.GetResponseStream(), IDisposable reader = new StreamReader(stream)) { result = (StreamReader)reader.ReadToEnd(); } 

    By the way, since using is used, it is not necessary to call close.

    • > By the way, since using is used, it is not necessary to call close. But is there a guarantee that in the destructor the thread will close itself? - AlexAndR 2:17 pm
    • 2
      Warranty is. If you're interested, try it yourself: using (FileStream fs = File.OpenWrite ("d: \ workdir \ file.txt")) {// Some actions on file.txt that are incredibly complex and confusing ...} File.Delete ("d: \ workdir \ file.txt"); Unclosed thread in the last line causes an exception :-) - mantigatos
    • It will work, but life has taught in such things to be unnecessarily meticulous. what works today may not work tomorrow (more than once it happened with the products of the same microsoft. and if they still support the documented features, the rest may change. Moreover, judging by this description, if you don’t call close, you can changes will not be saved:> When clearing a stream, the main encoder is not cleared until the Flush or Close implementation is explicitly invoked.
    • one
      using Statement (C # Reference) > The object provided the IDisposable interface. This interface provides the Dispose method. And further: Stream. Dispose of the resources. So yes, you can trust. - Ilya Pirogov

    As already mentioned, in the case of objects of the same type, you can simply list them separated by commas.

     using (A a1 = new A(), a2 = new A()) { //code } 

    In the case of objects of different types, the maximum allowable reduction is the lowering of curly brackets.

     using (A a = new A()) using (B b = new B()) { //code } 

    And the advantage of using this is precisely the impossibility of forgetting about the release of resources, since it will occur automatically when you exit the area. So your Close() is unnecessary here.

    • I would still use the close method, since I’ve got it, and since Microsoft itself uses it. such a careless attitude to the code is the cause of quite a few mistakes, IMHO. - AlexAndR
    • four
      Extract from the description of the Close() method from MSDN: "This method calls Dispose." The result is a double release. Microsoft, of course, was not too lazy to write protection against repeated calls, but I would not abuse it. - Jofsey 2:57 pm
    • one
      that's exactly what Close calls Dispose, and besides that, he can do a lot more. If, on the contrary, Dispose called Close, there would be no questions at all. By the way, funny, the code with msdn: isoFile.Dispose (); isoFile.Close (); - AlexAndR
     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { result = reader.ReadToEnd(); reader.Close(); } stream.Close(); } response.Close(); } 

    I understand this is equivalent to this code:

     HttpWebResponse response = (HttpWebResponse)request.GetResponse() { try { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { result = reader.ReadToEnd(); reader.Close(); } stream.Close(); } response.Close(); } finally { if (response != null) ((IDisposable)response).Dispose(); } } 

    everything inside the try block can be carried to a separate function.