8

Usually I was doing something like that (just a example):

using (Stream xmlStream = client.OpenRead(xmlUrl))
{
 using (XmlTextReader xmlReader = new XmlTextReader(xmlStream))
 {
 }
}

Isn't better to do just:

using (XmlTextReader xmlReader = new XmlTextReader(client.OpenRead(xmlUrl)))
{
}

But I'm not sure if in this short syntax all resources will be disposed (Stream) or only XmlTextReader?

Thanks in advance for your answer.

John Sibly
23.1k7 gold badges65 silver badges83 bronze badges
asked Mar 9, 2010 at 12:24
1
  • I actually like the first form of deleniation - with the brackets. It gets disposed properly and is most maintainable IMHO. Commented Mar 9, 2010 at 12:34

4 Answers 4

23

No; that won't guarantee that the Stream is disposed if the XmlTextReader constructor throws an exception. But you can do:

using (Stream xmlStream = client.OpenRead(xmlUrl))
using (XmlTextReader xmlReader = new XmlTextReader(xmlStream))
{
 // use xmlReader 
}
answered Mar 9, 2010 at 12:26
3
  • 1
    The first sentence is great (+1 for that) but the remainder, isn't this (and TomTom) just syntactic sugar? It's actually identical to the original code isn't it? Commented Mar 9, 2010 at 12:34
  • @Lazarus - it does the same thing, but (importantly) it stops the nesting from pushing the code off the right hand side of the screen ;-p In all seriousness, we're encapsulating a single unit here, so a single level of visual nesting seems desirable. Commented Mar 9, 2010 at 12:39
  • I didn't realise that this would stop the indent, it operates differently to an if statement which indents even if you omit the brackets for single line blocks. I learnt something... it's a good day ;) Commented Mar 9, 2010 at 15:57
12

With C# 8 you can get rid of even the single nesting level:

private static void NewMultipleUsingDeclarations()
{
 using var xmlStream = client.OpenRead(xmlUrl);
 using var xmlReader = new XmlTextReader(xmlStream);
 
 // use xmlReader 
}

Internally the compiler creates an equivalent try catch as with the indented version and disposes of both the stream and the reader at the end of the scope of the using variables, in this case, at the end of the method.

See more:

answered Apr 30, 2020 at 7:50
2

What about (I use this now):

using (Stream xmlStream = client.OpenRead(xmlUrl))
using (XmlTextReader xmlReader = new XmlTextReader(xmlStream))
{
...
}

The second using is the referenced using from the first - no need to have brackets.

answered Mar 9, 2010 at 12:27
2

The reference documentation indicates that the object to be disposed must be declared in the using statement. Since there is no declaration for the stream, the Dispose method will not be called.

In your case you could skip the stream entirely, though, and use the constructor for the TextReader that takes a url parameter. The underlying stream will be closed when the reader is disposed.

using (var xmlReader = new XmlTextReader( xmlUrl ))
{
 ...
}
answered Mar 9, 2010 at 12:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.