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.
-
I actually like the first form of deleniation - with the brackets. It gets disposed properly and is most maintainable IMHO.Mark Schultheiss– Mark Schultheiss2010年03月09日 12:34:53 +00:00Commented Mar 9, 2010 at 12:34
4 Answers 4
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
}
-
1The 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?Lazarus– Lazarus2010年03月09日 12:34:32 +00:00Commented 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.Marc Gravell– Marc Gravell2010年03月09日 12:39:52 +00:00Commented 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 ;)Lazarus– Lazarus2010年03月09日 15:57:27 +00:00Commented Mar 9, 2010 at 15:57
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:
- A more detailed description in Christian Nagel's blog on the new using declaration
- The official documentation.
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.
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 ))
{
...
}